Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Re: Reading and printing an XLSX file

by Tux (Abbot)
on Mar 21, 2019 at 09:02 UTC ( #1231523=note: print w/replies, xml ) Need Help??

in reply to Reading and printing an XLSX file

it being perl? Your boss?

If you make errors like these it is likely not do do what you expect it to do.

  • I used code tags
  • I restyled the code for readability
  • I fixed all obvious errors
  • I commented on the changes with # <-
  • I replaced the unmaintained parser with the right one: this is the main reason I reply.


use strict; # <- always use strict use warnings; # <- and warnings use Spreadsheet::WriteExcel; # <- missing declaration #use Spreadsheet::XLSX; # <- this module is *STRONGLY* di +scouraged! use Spreadsheet::ParseXLSX; # <- use this one instead #my $excel = Spreadsheet::XLSX->new ("/tmp/temp.xlsx"); # <- do not us +e Spreadsheet::XLSX my $excel = Spreadsheet::ParseXLSX->new->parse ("/tmp/temp.xlsx"); # print $excel; # <- you cannot simply print an object and hope it wri +tes a file # if you want to see what this is use Data::Peek or Data::Dumper #my @array; # <- unused #my $workbook = Excel::Writer::XLSX->new ("perl.xlsx"); # ^--- Your code uses ->addworksheet, which is Spreadsheet::WriteExcel + syntax and # not supported by Excel::Writer::XLSX, so you obviously do not w +ant this line my $FILENAME = "/tmp/Newfile.xls"; # <- used but commented out my $workbook = Spreadsheet::WriteExcel->new ($FILENAME); # <-- no qu +otes needed # ^--- variable used twice my $worksheet1 = $workbook->addworksheet ("Worksheet1"); $worksheet1->write ("A1", "Hi Excel!"); foreach my $sheet (@{$excel->{Worksheet}}) { print "Sheet: $sheet->{Name}\n"; # <- not really a good example fo +r printf $sheet->{MaxRow} ||= $sheet->{MinRow}; foreach my $row ($sheet->{MinRow} .. $sheet->{MaxRow}) { $sheet->{MaxCol} ||= $sheet->{MinCol}; foreach my $col ($sheet->{MinCol} .. $sheet->{MaxCol}) { my $cell = $sheet->{Cells}[$row][$col] or next; printf "(%3d, %3d) => %s\n", $row, $col, $cell->{Val}; my $temp = $cell->{Val}; $worksheet1->write ($row, $col, $cell->{Val}); } } last; # <- missing ; } # <- missing }

That code correctly created /tmp/Newfile.xls with one sheet named Worksheet1.

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Reading and printing an XLSX file
by jsuresh (Novice) on Mar 21, 2019 at 10:54 UTC

    Thank you so much for the support.

    I downloaded "Spreadsheet::ParseXLSX" module and added to my library.

    When I use "Spreadsheet::ParseXLSX" module. It gives me error

    Can't locate Spreadsheet/ in @INC (@INC contains: /opt/highdeal/mmahalingam/Spreadsheet/lib /opt/highdeal/lib/perl5 /usr/lib/perl5/site_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/site_perl/5.8.8 /usr/lib/perl5/site_perl /usr/lib/perl5/vendor_perl/5.8.8/i386-linux-thread-multi /usr/lib/perl5/vendor_perl/5.8.8 /usr/lib/perl5/vendor_perl /usr/lib/perl5/5.8.8/i386-linux-thread-multi /usr/lib/perl5/5.8.8 .) at line 7. BEGIN failed--compilation aborted at line 7.

    Please provide support.


      You are obviously using a rather old version of (system)perl: 5.8.8. You now have the following options (maybe even more, but to start with):

      1. Install the system package for Spreadsheet::ParseXLSX, on which I think the odds are very low that the system ever packaged that for your OS.
      2. Install using the toolset:
        • Using cpan
          $ sudo cpan Spreadsheet::ParseXLSX
        • Using cpanm:
          $ curl -L | perl - --sudo App::cpanminus $ cpanm -S +arseXLSX-0.27.tar.gz

      It is up to you to find if you want it installed outside of the default perl tree and what options the tools have to do so. The monestary is full of tips on how to use perl modules without the use of sudo.

      Enjoy, Have FUN! H.Merijn

      "...added to my library."

      How did you install this?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1231523]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2021-01-26 06:05 GMT
Find Nodes?
    Voting Booth?