How can I modify an existing Excel workbook with Perl?

user105033 picture user105033 · Sep 2, 2009 · Viewed 35.9k times · Source

With Spreadsheet::WriteExcel, I can create a new workbook, but what if I want to open an existing book and modify certain columns? How would I accomplish that?

I could parse all of the data out of the sheet using Spreadsheet::ParseExcel then write it back with new values in certain rows/columns using Spreadsheet::WriteExcel, however. Is there a module that already combines the two?

Mainly I just want to open a .xls, overwrite certain rows/columns, and save it.

Answer

Ether picture Ether · Sep 2, 2009

Spreadsheet::ParseExcel will read in existing excel files:

my $parser   = Spreadsheet::ParseExcel->new();
# $workbook is a Spreadsheet::ParseExcel::Workbook object
my $workbook = $parser->Parse('Book1.xls');

But what you really want is Spreadsheet::ParseExcel::SaveParser, which is a combination of Spreadsheet::ParseExcel and Spreadsheet::WriteExcel. There is an example near the bottom of the documentation.