2014-01-31 41 views
-1

我有一段代碼,其創建Excel工作表,並把該數據英寸以下是確實需要在打印以下數據到CSV文件

#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(); 

代碼我需要打印相同的數據與excel表格一起CSV文件。請幫助我,因爲我是perl的新手。

回答

1

這段代碼非常糟糕,這並沒有什麼幫助,而且當Perl的新手遇到類似這樣的問題時,這種情況並不會幫助。

它可以幫助您查看Text::CSV模塊,它將爲您完成大部分工作,但爲了使這更容易,您需要真正重構此代碼。

於是開始與這條線的位置:

$sheet->write($data_row,$data_col++,"$tfn",$number); 

你需要做的是從那些「寫」調用的獲取所有數據部分,構建一個陣列,每個單元作爲一個元素。然後,而不是調用每個單元格的寫入,而是使用write_row方法代替使用該數組的所有內容。這也是我們csv輸出的輸入方法。事情是這樣的:

$sheet->write_row(\@columns, $number); 
$csv->print($csv_file, \@columns); 

如果您在Perl data structures閱讀並重構哈希值是如何被建立,您還可以同時刪除大量的冗餘代碼在這裏,使這個更容易維護。

對於真正的未介紹你應該也知道如何install Perl modules

希望能給你一個開始。