我正在尝试解析excel文件中的数据(可以是xlsx或xls)。
我已经知道我想要获取哪些工作表,所以我想遍历它们并从中提取数据。
我的代码:
#!/usr/bin/perl -w
use strict;
use warnings;
use Spreadsheet::Read;
use Getopt::Long;
my $inputfile;
GetOptions (
'i=s' => \$inputfile,
);
die 'missing input file' unless $inputfile;
my $workbook = ReadData ($inputfile, debug => 9);
my @worksheets = (1);
foreach (@worksheets) {
my $sheet = $workbook->[$_-1];
next unless $sheet;
my ( $row_min, $row_max ) = $sheet->row_range();
my ( $col_min, $col_max ) = $sheet->col_range();
for my $row ($row_min .. $row_max) {
}
}
然而,我得到了以下信息:
Can't call method "row_range" on unblessed reference at perl/parse_test.pl line 22.
我是perl的新手,还不了解哈希、数组和引用的错综复杂。
发布于 2013-05-16 22:32:19
首先,如果您不需要数组,请不要使用它,
my @sheet = $workbook->[$_-1];
=> my $sheet = $workbook->[$_-1];
如果仍然发生错误,则打印检查$sheet
实例的$sheet
引用。
next unless $sheet;
print ref($sheet), "\n";
看起来您数据应该以另一种方式访问,这是从http://metacpan.org/pod/Spreadsheet::Read#Data-structure
$book = [
# Entry 0 is the overall control hash
{ sheets => 2,
sheet => {
"Sheet 1" => 1,
"Sheet 2" => 2,
},
type => "xls",
parser => "Spreadsheet::ParseExcel",
version => 0.59,
},
# Entry 1 is the first sheet
{ label => "Sheet 1",
maxrow => 2,
maxcol => 4,
cell => [ undef,
[ undef, 1 ],
[ undef, undef, undef, undef, undef, "Nugget" ],
],
A1 => 1,
B5 => "Nugget",
},
# Entry 2 is the second sheet
{ label => "Sheet 2",
:
:
]
所以如果你想从$sheet
中读取标签,它应该是$sheet->{label}
,依此类推。
发布于 2013-05-16 22:31:43
问题如下:
@sheet->row_range
不能在非对象上使用方法。对象必须是引用,即标量。它应该以$
开头,而不是@
。
https://stackoverflow.com/questions/16589295
复制相似问题