需要将以下数据打印到CSV文件中

时间:2014-01-31 07:23:20

标签: perl perl-module

我有一段代码可以创建excel表并将数据放入。以下是代码

    #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 个答案:

答案 0 :(得分:1)

这段代码非常糟糕,没有任何帮助,而且当Perl的新手遇到由Perl有限的人写的这样的东西时,通常会出现这种情况。

这将有助于您查看将为您完成大部分工作的Text::CSV模块,但为了使这更容易,您需要真正重构此代码。

所以从这一行开始:

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

您需要做的是从所有这些'write'调用中获取所有数据部分,并构造一个以每个单元格为元素的数组。然后,不是为每个单元格调用写入,而是使用write_row方法使用数组在一行中完成所有操作。这也将是我们的csv输出的输入方法。像这样:

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

如果您阅读了Perl data structures并重构了哈希的构建方式,您还可以在此处删除大量冗余代码,以使其更易于维护。

对于真正未引入的内容,您还应了解如何install Perl modules

希望能给你一个开始。