tux242 has asked for the wisdom of the Perl Monks concerning the following question:

I have been looking how to add a spreadsheet to a existing workbook in the docs and examples of this module, is there not an: my $workbook  = spreadsheet::WriteExcel->open("beads.xls"); type method anywhere? anyone know this one? I know the below code works for a new workbook, but i just want to add to an existing one...

my $workbook = spreadsheet::WriteExcel->new("beads.xls");

Replies are listed 'Best First'.
Re: open workbook-WriteExcel
by jmcnamara (Monsignor) on Nov 18, 2003 at 22:51 UTC

    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.

Re: open workbook-WriteExcel
by Thelonius (Priest) on Nov 18, 2003 at 19:13 UTC
    To quote from the documentation:
    This module cannot be used to write to an existing Excel file.
    Sorry. There is a ParseExcel module which can in principle be used, but not easily, and anything in the spreadsheet that is not supported by both modules would be lost when you copy it to a new workbook.

      you can use Win32::OLE to create that object. Below is a tested and verified example of how to add a sheet. If you want it to be named something specific you must later change the name property, as the worksheet cannot be named when it is first created.

      #!/perl -w use strict; use Win32::OLE; my $path = "C:\\TEST_EXCEL"; my $excel = new Win32::OLE('Excel.Application'); my $workbook = $excel->Workbooks->Open($path."\\Book1.xls"); $workbook->Worksheets->Add(); $workbook->Save(); $excel->Quit();

      Grygonos