用Perl写入Excel文件

时间:2015-03-25 13:39:26

标签: perl export-to-excel perl-module

这是我的第一次尝试,而且我试图了解使用的基础知识 Excel中::作家:: XLSX。我似乎无法让我的脚本编译。

#!C:\Perl\bin
#excel::writer attempt
#allows IR-Serial-Parts tracking

use strict;
use warnings;
use POSIX qw(strftime);
use Excel::Writer::XLSX;


my $ref = strftime '%Y-%m-%d', locatime();      #create the datestamp
my $file = "$ref.xlsx";
my $workbook = Excel::Writer::XLSX->new('$file');   #declare it outside of if loop preventing original issue.
#if(-e $file){
#       my $workbook = Excel::Writer::XLSX->open('$file');  #open existing  excel file
#}
#else{
#       my $workbook = Excel::Writer::XLSX->new('$file');   #open new Excel   if the date on comp has changed
#}

$worksheet = $workbook->add_worksheet("Tracking");
$worksheet->write( 'A1', 'Hi Excel!');

我甚至不得不做任何事情,但我只是想在电子表格中测试写入单元格,我甚至无法做到这一点。我觉得我现在正努力做到这一点。这是shell返回的内容。

Global symbol "$worksheet" requires explicit package name at writexcel.pl         line 20.
Global symbol "$workbook" requires explicit package name at writexcel.pl line   20
.
Global symbol "$worksheet" requires explicit package name at writexcel.pl      line 21.
Execution of writexcel.pl aborted due to compilation errors.
Press any key to continue . . .

现在原来的问题已经回答了。

Undefined subroutine &main::locatime called at writexcel.pl line 11.
Press any key to continue . . .

我的新错误。我认为它可能与strftime有关,但我不太确定。

3 个答案:

答案 0 :(得分:4)

my $workbook = Excel::Writer::XLSX->new('$file');

此行不会生成名称存储在$file变量中但文件名称为$file的文件。

答案 1 :(得分:3)

尝试在循环外声明$workbook变量。 此外,在您使用strict时,您需要使用$worksheet声明my

use strict;
use warninigs;

my $workbook;

if(-e $file){
        $workbook = Excel::Writer::XLSX->open($file);  
}
else{
        $workbook = Excel::Writer::XLSX->new($file); 
}

my $worksheet = $workbook->add_worksheet('Tracking');
$worksheet->write( 'A1', 'Hi Excel!');

答案 2 :(得分:0)

在循环外用“my”声明$ workbook和$ worksheet

参考https://github.com/AarthiRT/Excel_Writer_XLSX