Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

What I'm trying to do is query a database and send an email with the results as an attached excel file using MIME::Lite and SpreadSheet::WriteExcel. Using data dumper I can see the data returning from the query. What is happening is that the attached excel file has has one row, not multiple rows (I see them via the dumper print), containing things like "HASH(0x975700)". What do I need to change to get one row per query result, populated with the vaules which are printed in by data dumper?
use strict; use warnings; use DBI; use MIME::Lite; use Spreadsheet::WriteExcel; use Data::Dumper; my $ORACLE_HOME = '/u01/app/oracle/product/10.2.0/db_1/'; $ENV{'ORACLE_HOME'} = '/u01/app/oracle/product/10.2.0/db_1'; my $dsn = 'dns goes here'; my $usr = 'uid goes here'; my $pass = 'pass goes here'; my $sender = 'sending email goes here'; my $reciever = 'recieve email goes here'; my $OutputFile = '/tmp/test.xls'; my $workbook = Spreadsheet::WriteExcel->new($OutputFile); my $TestReport = $workbook->add_worksheet('TestReport'); my $dbh = DBI->connect( $dsn, $usr, $pass, { RaiseError => 1, AutoCommit => 0 }) || die "Database connection not made: $DBI::errstr"; my $TestReportSQL = "SELECT FOO,BAR, BAZ FROM TABLENAME"; my $sth = $dbh->prepare( $TestReportSQL ) or die "Can't prepare SQL: $ +DBI::errstr"; $sth->execute(); my @arr1 = $sth->fetchall_arrayref({}); print Dumper @arr1; $TestReport->write('A1', [\@arr1] ); $workbook->close(); my $msg = MIME::Lite->new( From => "$sender", To => "$reciever", Type => 'multipart/mixed', Subject => "Test"); $msg->attach( Type => "AUTO", Path => "$OutputFile", Filename => "test.xls", Disposition => "attachment" ) or die "Error attaching file $OutputFile : $!\n"; $msg->send();

Replies are listed 'Best First'.
Re: Fix and email Spreadsheet::WriteExcel results.
by Corion (Patriarch) on Aug 08, 2011 at 14:24 UTC

    Spreadsheet::WriteExcel::write() takes an arrayref of arrayrefs. DBI::fetchall_arrayref({}) returns an arrayref of hashrefs. Read the documentation of ->fetchall_arrayref to find out how to make it return array references.

    Also, you might be interested in Querylet.