Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic

Re: Excel how to open existing worksheet -- create and append to an XLS file

by Discipulus (Abbot)
on Feb 26, 2016 at 12:24 UTC ( #1156214=note: print w/replies, xml ) Need Help??

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 +"


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.
  • Comment on Re: Excel how to open existing worksheet -- create and append to an XLS file
  • Download Code

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2023-02-06 09:26 GMT
Find Nodes?
    Voting Booth?
    I prefer not to run the latest version of Perl because:

    Results (33 votes). Check out past polls.