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.

mpapec
  • 50,217
  • 8
  • 67
  • 127
Tony Xu
  • 3,031
  • 4
  • 32
  • 43
  • from the docs of [Spreadsheet::ParseExcel](https://metacpan.org/pod/Spreadsheet::ParseExcel#DESCRIPTION). The module cannot read files in the Excel 2007 Open XML XLSX format. See the [Spreadsheet::XLSX](https://metacpan.org/pod/Spreadsheet::XLSX) module instead. – clt60 Sep 25 '14 at 07:53

3 Answers3

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();
mpapec
  • 50,217
  • 8
  • 67
  • 127
  • Thanks for that and I got: No Excel data found. However, it confirms problem is $parser->parse. – Tony Xu Sep 25 '14 at 07:01
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() ) {

} 
Praveen
  • 902
  • 6
  • 21
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.

Tony Xu
  • 3,031
  • 4
  • 32
  • 43