I have a piece of code which creates an excel sheet and put the data in. The following is the code that does
#CREATE FORMATTING FOR EXCEL FILE
print DEBUG &now() . "Setting up the XLS document\n";
my $XLS_FILE = "$WORK/cos_call_prompter79717_$CPREVDATE.xls";
print "CREATE XLS FILE\n";
my $rpt=Spreadsheet::WriteExcel->new("$XLS_FILE");
my $title_heading1 = $rpt->addformat();
$title_heading1->set_size(14);
$title_heading1->set_bold();
$title_heading1->set_align('center');
my $title_heading2 = $rpt->addformat();
$title_heading2->set_size(12);
$title_heading2->set_bold();
$title_heading2->set_align('center');
my $heading1 = $rpt->addformat();
$heading1->set_size(9);
$heading1->set_bold();
$heading1->set_align('center');
$heading1->set_border(1);
$heading1->set_pattern(1);
$heading1->set_fg_color('43');
my $description = $rpt->addformat();
$description->set_size(9);
$description->set_align('center');
$description->set_border(1);
my $number = $rpt->addformat();
$number->set_size(8);
$number->set_align('right');
$number->set_border(1);
$number->set_num_format('#,##0');
# PRINT THE REPORTS
print DEBUG &now() . "Print the XLS document\n";
my $row = 5;
my $freeze_row;
$freeze_row = $row;
$sheet=$rpt->addworksheet("TFN Breakout");
$sheet->set_column(0,0,16);
$sheet->set_column(1,16,20);
$sheet->hide_gridlines(2);
$sheet->set_landscape();
$sheet->set_margins_LR(.8);
$sheet->set_margins_TB(.8);
$sheet->write(0,0, "COS Call Prompter - 79717", $title_heading1);
$sheet->merge_cells("A1:P1","",$title_heading1);
$sheet->write(1,0, "BAC TFN_Breakout - $MM/$DD/$CC$YY", $title_heading1);
$sheet->merge_cells("A2:P2","",$title_heading1);
$sheet->write(2,0, "Path Chosen - all active 8YY number need to be logged", $tittle_heading1);
$sheet->merge_cells("A3:P3","",$title_heading1);
$sheet->write(4,0, "TFN",$heading1);
$sheet->write(4,1, "YTD",$heading1);
$sheet->write(4,2, "Status/YTD",$heading1);
$sheet->write(4,3, "Telesale/YTD",$heading1);
$sheet->write(4,4, "Customer Service/YTD",$heading1);
$sheet->write(4,5, "Attribute Path/YTD",$heading1);
$sheet->write(4,6, "Daily",$heading1);
$sheet->write(4,7, "Status/Daily",$heading1);
$sheet->write(4,8, "Telesales/Daily",$heading1);
$sheet->write(4,9, "Customer Service/Daily",$heading1);
$sheet->write(4,10, "Attribute Path/Daily",$heading1);
$sheet->write(4,11, "MTD",$heading1);
$sheet->write(4,12, "Status/MTD",$heading1);
$sheet->write(4,13, "Telesales/MTD",$heading1);
$sheet->write(4,14, "Customer Service/MTD",$heading1);
$sheet->write(4,15, "Attribute Path/MTD",$heading1);
$sheet->freeze_panes($freeze_row,1);
# print data
print DEBUG &now() . "print data to the XLS document\n";
my $data_row;
$data_row = $row;
my $data_col;
foreach my $tfn (sort keys %ytdincoptcounts)
{
$data_col = 0;
### Write the TFN for the given APN down the rows
$sheet->write($data_row,$data_col++,"$tfn",$number );
### Verify all data is set or default to zero
if(!defined($ytdincoptcounts{$tfn}{'145'})) { $ytdincoptcounts{$$tfn}{'145'} = 0; }
if(!defined($ytdincoptcounts{$tfn}{'116'})) { $ytdincoptcounts{$$tfn}{'116'} = 0; }
if(!defined($ytdincoptcounts{$tfn}{'144'})) { $ytdincoptcounts{$$tfn}{'144'} = 0; }
if(!defined($ytdincoptcounts{$tfn}{'402'})) { $ytdincoptcounts{$$tfn}{'402'} = 0; }
if(!defined($dtdincoptcounts{$tfn}{'145'})) { $dtdincoptcounts{$$tfn}{'145'} = 0; }
if(!defined($dtdincoptcounts{$tfn}{'116'})) { $dtdincoptcounts{$$tfn}{'116'} = 0; }
if(!defined($dtdincoptcounts{$tfn}{'144'})) { $dtdincoptcounts{$$tfn}{'144'} = 0; }
if(!defined($dtdincoptcounts{$tfn}{'402'})) { $dtdincoptcounts{$$tfn}{'402'} = 0; }
if(!defined($mtdincoptcounts{$tfn}{'145'})) { $mtdincoptcounts{$$tfn}{'145'} = 0; }
if(!defined($mtdincoptcounts{$tfn}{'116'})) { $mtdincoptcounts{$$tfn}{'116'} = 0; }
if(!defined($mtdincoptcounts{$tfn}{'144'})) { $mtdincoptcounts{$$tfn}{'144'} = 0; }
if(!defined($mtdincoptcounts{$tfn}{'402'})) { $mtdincoptcounts{$$tfn}{'402'} = 0; }
### Sum up the three call types then write to file
my $ytd_total = $ytdincoptcounts{$tfn}{'145'} + $ytdincoptcountss{$tfn}{'116'} + $ytdincoptcounts{$tfn}{'144'} + $ytdincoptcounts{$tfn}{'402'};
my $dtd_total = $dtdincoptcounts{$tfn}{'145'} + $dtdincoptcountss{$tfn}{'116'} + $dtdincoptcounts{$tfn}{'144'} + $dtdincoptcounts{$tfn}{'402'};
my $mtd_total = $mtdincoptcounts{$tfn}{'145'} + $mtdincoptcountss{$tfn}{'116'} + $mtdincoptcounts{$tfn}{'144'} + $mtdincoptcounts{$tfn}{'402'};
$sheet->write($data_row,$data_col++,$ytd_total,$number);
$sheet->write($data_row,$data_col++,$ytdincoptcounts{$tfn}{'145''},$number);
$sheet->write($data_row,$data_col++,$ytdincoptcounts{$tfn}{'116''},$number);
$sheet->write($data_row,$data_col++,$ytdincoptcounts{$tfn}{'144'},$nnumber);
$sheet->write($data_row,$data_col++,$ytdincoptcounts{$tfn}{'402''},$number);
$sheet->write($data_row,$data_col++,$dtd_total,$number);
$sheet->write($data_row,$data_col++,$dtdincoptcounts{$tfn}{'145''},$number);
$sheet->write($data_row,$data_col++,$dtdincoptcounts{$tfn}{'116''},$number);
$sheet->write($data_row,$data_col++,$dtdincoptcounts{$tfn}{'144'},$nnumber);
$sheet->write($data_row,$data_col++,$dtdincoptcounts{$tfn}{'402''},$number);
$sheet->write($data_row,$data_col++,$mtd_total,$number);
$sheet->write($data_row,$data_col++,$mtdincoptcounts{$tfn}{'145''},$number);
$sheet->write($data_row,$data_col++,$mtdincoptcounts{$tfn}{'116''},$number);
$sheet->write($data_row,$data_col++,$mtdincoptcounts{$tfn}{'144'},$nnumber);
$sheet->write($data_row,$data_col++,$mtdincoptcounts{$tfn}{'402''},$number);
csvlog("
$data_row++;
}
$rpt->close();
i need to print the same data in to CSV file along with the excel sheet. Please help me on this as i am new to perl.
해결책
The code is pretty horrible which doesn't help, and is often the case which doesn't help when a newcomer to Perl encounters something like this that was written by someone with limited Perl.
It would help you to look at the Text::CSV module which will do most of the work for you, but in order to make this easier you need to really refactor this code.
What you need to do is get all of the data parts from all of those 'write' calls and construct an array with each cell as an element. Then instead of calling a write for every cell you can do it all in one line using the array using the write_row method instead. This is also going to be the input method for our csv output. Something like this:
If you read up on Perl data structures and refactor how the hashes are being built you can also also remove a lot of redundant code here to make this more maintainable.
For the really un-introduced you should also be aware of how to install Perl modules.