http://www.perlmonks.org?node_id=1156214


in reply to Excel how to open existing worksheet

Ok i came acroos this thread because i wanted to edit an existing xlsx file. I jumped directly onto Excel::Writer::XLSX as is the new standard to write xlsx files. After some misleading errors i read in the docs of the module:
This module cannot, as yet, be used to write to an existing Excel XLSX file.

Ouch! when i realized was not possible i choosed that writing to an existing xls (old format file) would suffice for my tests and i ended with the following code. A mix of Spreadsheet::ParseExcel::SaveParser and Spreadsheet::WriteExcel worked out fine. I put it here for future memory.

use strict; use warnings; use Spreadsheet::ParseExcel::SaveParser; use Spreadsheet::WriteExcel; # check for right parameters my ($file,$howlong,$sleep) = @ARGV; unless ( defined $file && $file =~/.+\.xls/ && defined $howlong && $howlong =~/^\d+$/ && defined $sleep && $sleep =~/^\d+$/){ die "USAGE: $0 filename.xls minutes_of_duration sleep_seconds", "\n\n\t$0 file.xls 5 1\n\n", "will creates file.xls and each seconds writes to it, for 5 m +inutes.\n"; } $howlong *=60; $howlong = $^T+$howlong; print "\tNB: $0 will run until ",scalar localtime($howlong),"\n"; my ($col,$row); $col = $row = 0; # initialize ta new XLS file my $workbook_init = Spreadsheet::WriteExcel->new($file); my $worksheet_init = $workbook_init->add_worksheet(); $worksheet_init->write( $row, $col, "XLS initialized at ".scalar local +time(time)); $workbook_init->close(); $row++; # modify the same xls in the while loop while (time <= $howlong){ print "writing row $row\n"; my $parser = Spreadsheet::ParseExcel::SaveParser->new(); my $template = $parser->Parse($file); my $worksheet = $template->worksheet(0); $worksheet->AddCell( $row, $col, scalar localtime (time) ); $template->SaveAs($file); ++$row; sleep $sleep; } print "\treached ",(scalar localtime(time)),"\n\tscheduled termination +"

L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.