Home > OS >  How to read Excel file in Spreadsheet::ParseXLSX
How to read Excel file in Spreadsheet::ParseXLSX

Time:07-09

I'm trying to read .xlsx file in Spreadsheet::ParseXLSX

#!/home/bin/perl

use strict;
use warnings;

use Spreadsheet::ParseXLSX;

my $oExcel = new Spreadsheet::ParseExcel;
die "You must provide a filename to $0 to be parsed as an Excel file"
unless @ARGV;


my $oBook = $oExcel->Parse($ARGV[0]);
my($iR, $iC, $oWkS, $oWkC);
print "FILE  :", $oBook->{File} , "\n";
print "COUNT :", $oBook->{SheetCount} , "\n";
print "AUTHOR:", $oBook->{Author} , "\n" if defined $oBook->{Author};

When I execute the perl, then I got the message as below,

Use of uninitialized value in print at conv2xlsxml.pl line 64.
FILE  :
Use of uninitialized value in print at conv2xlsxml.pl line 65.
COUNT :

How do I correctly access excel file in Spreadsheet::ParseXLSX ?

CodePudding user response:

Where did you read that was the correct way to use SpreadSheet::ParseXLSX?

I don't know the module at all well but, reading the documentation, it says:

This module returns data using classes from Spreadsheet::ParseExcel, so for the most part, it should just be a drop-in replacement.

And reading the documentation for Spreadsheet::ParseExcel, I don't see any examples that access the data inside the object directly in the way that your code does. Instead, there are methods to access the data inside the workbook object.

Workbook

A Spreadsheet::ParseExcel::Workbook is created via the Spreadsheet::ParseExcel parse() method:

my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Book1.xls');

The main methods of the Workbook class are:

$workbook->worksheets()
$workbook->worksheet()
$workbook->worksheet_count()
$workbook->get_filename()

These more commonly used methods of the Workbook class are outlined below. The other, less commonly used, methods are documented in Spreadsheet::ParseExcel::Worksheet.

So, instead of $oBook->{File}, it looks like you should be called $oBook->getfilename() and instead of $oBook->{SheetCount}, it looks like you should be calling $oBook->worksheet_count(). I can't see any obvious way to get the author of the spreadsheet.

Oh, and seeing you using new Spreadsheet::ParseExcel makes me think you're using very old documentation. That's "indirect object notation" and we've been recommending you don't use it for about twenty years. And, of course, you should be making a Spreadsheet::ParseXLSX object, not a Spreadsheet::ParseExcel object.

my $oBook = Spreadsheet::ParseXLSX->new;
  •  Tags:  
  • perl
  • Related