Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Modifying an .xlsx file (Excel 2007 and newer)

Tags:

excel

perl

xlsx

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

edit: 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.

Thanks you for your help :)

like image 597
MiSo Avatar asked Sep 19 '13 15:09

MiSo


1 Answers

The Spreadsheet::XLSX module is a Parser, i.e. it is not supposed to write files, only to read and understand them.

To write the file back to disk, I suggest you the Excellent (no pun intended :) ) Excel::Writer::XLSX by John McNamara.

http://search.cpan.org/~jmcnamara/Excel-Writer-XLSX/lib/Excel/Writer/XLSX.pm

Basically you can slurp (via Spreadsheet::XLSX) the XLSX file into a multilevel hash reference structured like this:

$xlsx_as_read->{worksheet_name}->{column}->{row}=value;

Modifying the example on the CPAN link you posted:

use Text::Iconv;
my $converter = Text::Iconv -> new ("utf-8", "windows-1251");

# Text::Iconv is not really required.
# This can be any object with the convert method. Or nothing.

use Spreadsheet::XLSX;
my $xlsx_as_read;

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

foreach my $sheet (@{$excel -> {Worksheet}}) {

       printf("Sheet: %s\n", $sheet->{Name});
       $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];
                       if ($cell) {
                           $xlsx_as_read->{$sheet->{Name}}->{$col}->{$row}=$cell -> {Val};
                       }
               } 
       }
}

and then pour it back to an Excel::Writer::XLSX workbook, which can be written to disk.

my $new_workbook = Excel::Writer::XLSX->new( 'output_file_name.xlsx' );
#populate cells with a loop similar to the one before, 
#iterating on $xlsx_as_read
like image 177
RafDouglas Avatar answered Sep 17 '22 06:09

RafDouglas