Not a HASH reference at /usr/share/perl5/Spreadsheet/WriteExcel/Worksheet.pm line 1798. #### #!/usr/bin/perl -T use strict; use warnings; use Carp::Assert; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use lib '/home/shawn/test/ais'; use DBConnect; my( $infile ) = $ARGV[ 0 ] =~ m/^([\ A-Z0-9_.-]+)$/ig; my( $date ) = $ARGV[ 1 ] =~ m/^([0-9-]+)$/ig; print "infile = $infile\n"; my $sth = $dbh->prepare( 'SELECT * FROM table WHERE field = ? AND YEAR(time) = ?' ) or die "QUERY FAIL: $DBI::errstr\n$!\n"; my @header = ([ "Some", "header", "fields" ]); my $parser = Spreadsheet::ParseExcel->new(); my $workbookin = $parser->parse( $infile ); if ( !defined $workbookin ) { die "Can\'t read spreadsheet: ", $parser->error(), ".\n"; } for( my $sheetcount = 0; $sheetcount < $workbookin->{SheetCount}; $sheetcount++ ) { my($name, $indata, $width) = readws( $sheetcount ); my $outfile = $name =~ /^([\ A-Z0-9_-]+)$/; my $workbookout = Spreadsheet::WriteExcel->new( $outfile . ".xls" ); my $worksheetout = $workbookout->add_worksheet( 'Data' ); $worksheetout->write_col( 0, 0, @$indata ); foreach my $row ( 1 .. @$indata) { my $sqldata = sqlget( $indata->[ $row ]->[ 3 ] ); if( $sqldata->[ 0 ] ) { my $worksheetout = $workbookout->add_worksheet( $indata->[ $row ]->[ 0 ] ); foreach my $col ( @$width ) { $worksheetout->set_column( $col, $col, $width->[ $col ] ); } $worksheetout->write_col( 0, 0, \@header ); $worksheetout->write_col( 1, 0, @$sqldata ); } } } sub readws { # Read worksheet - return worksheet name, data, and max cell width my $wsnum = $_[ 0 ]; # Count of input worksheet my $worksheetin = $workbookin->{Worksheet}[ $wsnum ]; my( @xldata, @width ); my( $row_min, $row_max ) = $worksheetin->row_range(); my( $col_min, $col_max ) = $worksheetin->col_range(); my $wsname = $worksheetin->{Name}; # Name of worksheet for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { $width[ $col ] = 0 unless $width[ $col ]; my $cell = $worksheetin->get_cell( $row, $col ); next unless $cell; $xldata[ $row ][ $col ] = $cell->unformatted() ; if( length( $xldata[ $row ][ $col ] ) > $width[ $col ] ) { $width[ $col ] = length( $xldata[ $row ][ $col ] ); } } } return( $wsname, \@xldata, \@width ); } sub sqlget { # Return sql data my $sqlin = $_[ 0 ]; $sth->execute( $sqlin, $date ); my $sqlout = $sth->selectall_arrayref; if( $sth->rows > 0) { return $sqlout; } else { return undef; } }