2014-01-31 1 views
-1

У меня есть кусок кода, который создает лист первенствовать и поместить данные в. Ниже приведен код, который делаетнужно напечатать следующие данные в 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(); 

мне нужно напечатать то же самое данные в CSV-файл вместе с листом excel. Пожалуйста, помогите мне в этом, поскольку я новичок в perl.

ответ

1

Код довольно ужасный, что не помогает, и часто бывает так, что не помогает, когда новичок на Perl сталкивается с чем-то подобным, написанным кем-то с ограниченным 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.

Надеюсь, что дает вам начало.

Смежные вопросы