Search code examples
perlxlsx

How to read data from xlsx in perl


Need a few good people help me with reading excel file with extension "xlsx" my script works for "xls" but not "xlsx", here is the code I get error: Can't call method "worksheet" on an undefined value if the file is "xlsx" here is the code I do have:

#!/usr/bin/perl -w

use warnings;
use strict;
use Spreadsheet::ParseExcel;
use Spreadsheet::XLSX;
use Date::Format;

my $filename = "../test.xlsx";
#Parse excel file
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse("$filename");

#Get cell value from excel sheet1 row 1 column 2
my $worksheet = $workbook->worksheet('Sheet1');
my $cell = $worksheet->get_cell(0,1);

# Print the cell value when not blank
if ( defined $cell and $cell->value() ne "") {
    my $value = $cell->value();
    print "cell value is $value \n";
}

Solution

  • Spreadsheet::XLSX is an close equivalent of Spreadsheet::ParseExcel for .xlsx files; you need to say:

    my $parser = Spreadsheet::XLSX->new();
    

    instead of using ParseExcel.