1

Just installed Spreadsheet::ParseExcel module, but got problem. See a few related question, but not exact the same.

#!/usr/bin/perl -w                                                                                        

use strict;
use warnings;

use Spreadsheet::ParseExcel;
use Data::Dumper;

my $bill = $ARGV[0] || die "Usage: $0 bill.xlsx\n";
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book2.xlsx');
print Dumper $workbook;

for my $worksheet ( $workbook->worksheet() ) { }

When I run the perl script, I got following error:

Can't call method "worksheet" on an undefined value at read.bill.xlsx.pl line 15.

So I dumped $workbook, and got:

$VAR1 = undef;

and dumper parser shows some data. This means $parser->parse failed. Anybody know why? Thanks.

1
  • from the docs of Spreadsheet::ParseExcel. The module cannot read files in the Excel 2007 Open XML XLSX format. See the Spreadsheet::XLSX module instead. Commented Sep 25, 2014 at 7:53

3 Answers 3

2

As documentation suggest, you have to test if parse() returned undef and check error message,

my $workbook = $parser->parse('Book2.xlsx') // die $parser->error();
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for that and I got: No Excel data found. However, it confirms problem is $parser->parse.
2

Try this:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;

my $bill = $ARGV[0] || die "Usage: $0 bill.xlsx\n";
my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book2.xls');

if ( !defined $workbook ) {
    die $parser->error(), ".\n";
}

for my $worksheet ( $workbook->worksheets() ) {

} 

Comments

2

After googling around, I found XLSX format requires Spreadsheet::XLSC instead of ParseExcel. Also the parse function take the filename then no need the step of $parser->parse.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.