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.


In reply to Re: open workbook-WriteExcel by jmcnamara
in thread open workbook-WriteExcel by tux242

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.