0

我正在尝试在单个 perl 程序中读取 2 个 excel 表。

像这样 -->

#!/usr/bin/perl -w

use strict;
use IO::File;
use IO::Seekable;
use Spreadsheet::ParseExcel;

my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('FILE1.xls');
my $worksheet1 = $workbook->worksheet('Worksheet_name1');

my $workbookB  = $parser->Parse('FILE2.xls');
my $worksheetB = $workbookB->worksheet('Worksheet_name2');

但是,我面临这个错误 - 无法在 ./PERL_PROGRAM.pl 的未定义值上调用方法“工作表”

有人可以给我一些指示.. 谢谢你的帮助

4

1 回答 1

2

如果方法失败,则该parse方法返回undef,而您无需检查。(这就是你得到Can't call method "worksheet" on an undefined value. 的原因。)另外,它是parse,不是Parse(Perl 区分大小写)。尝试这个:

#!/usr/bin/perl

use strict;
use warnings;
use Spreadsheet::ParseExcel;

my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('FILE1.xls') or die $parser->error;
my $worksheet1 = $workbook->worksheet('Worksheet_name1')
                 or die "Worksheet_name1 doesn't exist";

my $workbookB  = $parser->parse('FILE2.xls') or die $parser->error;
my $worksheetB = $workbookB->worksheet('Worksheet_name2')
                 or die "Worksheet_name2 doesn't exist";
于 2012-05-07T06:29:41.107 回答