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

Hello all, I am trying to use a hash to efficiently create multiple Excel workbooks with different worksheets in each. Unfortunately, while the over all workbooks are created, and I don't see any errors, I can't get information written into the actual worksheets. Any help here would be appreciated. The following sample code creates three different workbooks, each with one worksheet. The worksheets should each be named "Test," but that doesn't work. Also, the string "TEST TEST" should be written into A1 on each worksheet.
#!/usr/bin/perl use Getopt::Std; use Time::Local; use Spreadsheet::WriteExcel; use OLE::Storage_Lite; my $data_directory="."; my $SP_summary_filename=""; my $LUN_summary_filename=""; my $DISK_summary_filename=""; my $SP_workbook=""; my $LUN_workbook=""; my $DISK_workbook=""; my $ge_sec=""; my $ge_min=""; my $ge_hour=""; my $ge_mday=""; my $ge_mon=""; my $ge_year=""; my $ge_wday=""; my $ge_yday=""; my $ge_isdist=""; ($ge_sec,$ge_min,$ge_hour,$ge_mday,$ge_mon,$ge_year,$ge_wday,$ge_y +day,$ge_isdst)=localtime(time); $SP_summary_filename=sprintf("NARA_SP_"."%4d-%02d-%02d_%02d%02d%02d.xl +s",$ge_year+1900,$ge_mon+1,$ge_mday,$ge_hour,$ge_min,$ge_sec); $LUN_summary_filename=sprintf("NARA_LUN_"."%4d-%02d-%02d_%02d%02d%02d. +xls",$ge_year+1900,$ge_mon+1,$ge_mday,$ge_hour,$ge_min,$ge_sec); $DISK_summary_filename=sprintf("NARA_Disk_"."%4d-%02d-%02d_%02d%02d%02 +d.xls",$ge_year+1900,$ge_mon+1,$ge_mday,$ge_hour,$ge_min,$ge_sec); $INFO=SP; $AH{'SS'}{$INFO}{'POINTER'}=Spreadsheet::WriteExcel->new($SP_summary_f +ilename); $AH{'SS'}{$INFO}{'SS_NAME'}=$SP_summary_filename; $INFO=LUN; $AH{'SS'}{$INFO}{'POINTER'}=Spreadsheet::WriteExcel->new($LUN_summary_ +filename); $AH{'SS'}{$INFO}{'SS_NAME'}=$LUN_summary_filename; $INFO=DISK; $AH{'SS'}{$INFO}{'POINTER'}=Spreadsheet::WriteExcel->new($DISK_summary +_filename); $AH{'SS'}{$INFO}{'SS_NAME'}=$DISK_summary_filename; for $INFO (keys %{$AH{'SS'}}) { print "The spreadsheet is $AH{'SS'}{$INFO}{'SS_NAME'}.\n"; $AH{'SS'}{$INFO}{'top_left_header_format'}=$AH{'SS'}{$INFO}{'POINTER'} +->add_format(); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_color('white'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_bg_color('navy'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_font('Arial'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_size(8); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_bold(1); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_text_wrap(1); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_align('center'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_valign('vcenter'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_left('2'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_top('2'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_bottom('1'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_right('1'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_left_color('8'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_top_color('8'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_bottom_color('9'); $AH{'SS'}{$INFO}{'top_left_header_format'}->set_right_color('9'); $AH{'SS'}{$INFO}{'FIRST_WS'} = $AH{'SS'}{$INFO}{'POINTER'}->add_worksh +eet("Test"); $AH{'SS'}{$INFO}{'FIRST_WS'}->set_column('A:A', 2); $AH{'SS'}{$INFO}{'FIRST_WS'}->write_string('A1',"TEST TEST",$AH{'SS'}{ +$INFO}{'top_left_header_format'}); print "For fun:\n"; print "$AH{'SS'}{$INFO}{'POINTER'}\n"; print "$AH{'SS'}{$INFO}{'top_left_header_format'}\n"; print "$AH{'SS'}{$INFO}{'FIRST_WS'}\n"; print "End of pass.\n"; print "\n"; }
UPDATE: Apparently in this situation you actually need to "close" the workbook. The docs say that the workbook will close on program execution. So, I'll I did was make the following change:
...... print "End of pass.\n"; print "\n"; $AH{'SS'}{$INFO}{'POINTER'}->close(); }

Replies are listed 'Best First'.
Re: Hash with WriteExcel
by toolic (Bishop) on May 31, 2009 at 01:32 UTC
    Add use warnings; use strict;, then fix the errors. It seems to work for me: I get a "Test" worksheet, and the string "TEST TEST" is written into A1 on each worksheet.

    Update: also consider simplifying your code using strftime from POSIX: