sogo87 has asked for the wisdom of the Perl Monks concerning the following question:

I successfully parsed an xls file using Spreadsheet::ParseExcel::SaveParser and modified it with Spreadsheet::WriteExcel.

However working with xlsx file is a whole different thing. I am trying to figure out how to work with Spreadsheet::XLSX for parsing and how to make it work with Excel::Writer::XLSX. Spreadsheet::ParseExcel::SaveParser has a SaveAs() method that makes it possible to apply Spreadsheet::WriteExcel methods on the parsed xml file, but I don't understand how to make it work with xlsx file

When using Spreadsheet::ParseExcel::SaveParser and Spreadsheet::WriteExcel I can write:

#!/usr/bin/perl -w use strict; use Spreadsheet::ParseExcel; use Spreadsheet::ParseExcel::SaveParser; # Open the template with SaveParser my $parser = new Spreadsheet::ParseExcel::SaveParser; my $template = $parser->Parse('template.xls'); # Rewrite the file or save as a new file $workbook = $template->SaveAs('new.xls'); # Use Spreadsheet::WriteExcel methods my $worksheet = $workbook->sheets(0); $worksheet->write($row+2, $col, "World2"); $workbook->close();

I would like to do the same with xlsx files. therefore I'm trying to use Spreadsheet::XLSX and Excel::Writer::XLSX. Instead of:

my $parser = new Spreadsheet::ParseExcel::SaveParser; my $template = $parser->Parse('template.xls');

I use

my $excel = Spreadsheet::XLSX -> new ('test.xlsx');

Now, after parsing the xlsx file I would like to add some data to it and I don't know how to do it. As you can see above when using Spreadsheet::ParseExcel::SaveParser I used SaveAs() function, but Spreadsheet::XLSX dosn't have a SaveAs() method. So how do I add data to parsed xlsx file?

I could not find an answer to my question in this link: http://search.cpan.org/~dmow/Spreadsheet-XLSX-0.13-withoutworldwriteables/lib/Spreadsheet/XLSX.pm Thanks you for your help :)

Replies are listed 'Best First'.
Re: Modifying xlsx file with perl
by kcott (Archbishop) on Oct 02, 2013 at 18:44 UTC

    G'day sogo87,

    Welcome to the monastery.

    Spreadsheet::XLSX is for reading only.

    Excel::Writer::XLSX is for writing; however, it can only write to newly created files. The fourth line of the DESCRIPTION says:

    "This module cannot, as yet, be used to write to an existing Excel XLSX file."

    So, what you'll probably need to do is read the existing file, create a new file, then write the original data along with any modifications or additions. The basic code would look something like:

    for my $read_sheet ... for my $row ... for my $col ... my $cell = $read_sheet->{Cells}[$row][$col]; $write_sheet->write($row, $col, $cell->{Val}); ... ... ...

    -- Ken

Re: Modifying xlsx file with perl
by hdb (Monsignor) on Oct 02, 2013 at 20:12 UTC

    Should you be on Windows and have Excel on your machine, have a look at Win32::OLE. There is a lot of material on PerlMonks and you can do everything that Excel can.

Re: Modifying xlsx file with perl
by realflash (Acolyte) on Jul 22, 2024 at 19:52 UTC
    There is now a module that helps you convert data read with Spreadsheet::ParseXLSX into an object that Excel::Writer::XLSX can write. It would be nice if there was one module that did all this, but there isn't as best as I can tell. So for now you must use three modules to get this done. See Excel-CloneXLSX-Format