我可以在perl中的foreach中创建Excel工作簿吗?

时间:2014-01-09 19:29:20

标签: excel perl

我想创建多个Excel文件。文件将输出基本相同的格式,这是不同年份数据的唯一区别。

如果我按以下方式运行程序,它会运行并创建excel文件而不会出现问题:

use warnings;
use Excel::Writer::XLSX;
use Date::Parse;

... ###some validation of the data to work with
... ### put data on hashes
...

 my $workbook  = Excel::Writer::XLSX->new( "Monitoring_Report_2013.xlsx" );
 $worksheet1  = $workbook->add_worksheet('Q1');
 $worksheet2  = $workbook->add_worksheet('Q2');
 $worksheet3  = $workbook->add_worksheet('Q3');
 $worksheet4  = $workbook->add_worksheet('Q4');

... ### create the different tables on each worksheet
...
...

如果我添加foreach部分,那么它可以自动创建每年运行的不同文件,但是当我尝试打开excel文件时,它会生成一个损坏的错误。

use warnings;
use Excel::Writer::XLSX;
use Date::Parse;

...
...
...


my @years_in_data = ("2012", "2013", "2014");


foreach my $year(@years_in_data)
{
    chomp $year;

    ...
    ...
    ...

    my $workbook  = Excel::Writer::XLSX->new( "Monitoring_Report_$year.xlsx" );
    $worksheet1  = $workbook->add_worksheet('Q1');
    $worksheet2  = $workbook->add_worksheet('Q2');
    $worksheet3  = $workbook->add_worksheet('Q3');
    $worksheet4  = $workbook->add_worksheet('Q4');

    ...
    ...
    ...
}

我可以自动创建文件,还是需要手动编写每个文件?

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

你是否为每个人调用了$ worksheet-> write()方法?也许你说的是你做的所有点。如果你不这样做,我就看不到他们都没有写过。