Due to the binary nature of the Excel file format you cannot use Spreadsheet::WriteExcel to write or add to an existing file.
However, you can use Spreadsheet::ParseExcel to read an Excel file and then use Spreadsheet::WriteExcel to rewrite it.
The Spreadsheet::ParseExcel package also contains a module called Spreadsheet::ParseExcel::SaveParser which will let you read and rewrite an Excel workbook. It is a combination of Spreadsheet::WriteExcel and Spreadsheet::ParseExcel.
Here is an example:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel::SaveParser;
# Open an existing file with SaveParser
my $parser = new Spreadsheet::ParseExcel::SaveParser;
my $template = $parser->Parse('template.xls');
my $sheet = 0;
my $row = 0;
my $col = 0;
# Get a format from a cell
my $format = $template->{Worksheet}[$sheet]
->{Cells}[$row][$col]
->{FormatNo};
# Write data to some cells
$template->AddCell(0, $row, $col, 1, $format);
$template->AddCell(0, $row+1, $col, "Hello", $format);
# Add a new worksheet
$template->AddWorksheet('New Data');
# The SaveParser SaveAs() method returns a reference to a
# Spreadsheet::WriteExcel object. If you wish you can then
# use this to access any of the methods that aren't
# available from the SaveParser object. If you don't need
# to do this just use SaveAs().
#
my $workbook;
{
# SaveAs generates a lot of harmless warnings about unset
# Worksheet properties. You can ignore them if you wish.
local $^W = 0;
# Rewrite the file or save as a new file
$workbook = $template->SaveAs('newfile.xls');
}
# Use Spreadsheet::WriteExcel methods
my $worksheet = $workbook->sheets(0);
$worksheet->write($row+2, $col, "World");
__END__
Note, however that this will only read and rewrite the features that Spreadsheet::ParseExcel and Spreadsheet::WriteExcel can handle so macros, graphs and some other features in the original Excel file will be lost.
--
John.
|