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

Does anybody know the best way of editing an excel file?

I installed the spreadsheet::writeexcel and parseexcel modules but it doesn't seem like you can open existing file and then edit it. parsing the entire file and then rewriting the entire file is too inefficient.

suggestions?

Replies are listed 'Best First'.
Re: Excel from Perl
by bikeNomad (Priest) on Aug 07, 2001 at 07:56 UTC
    But that's exactly the model you have to use given the typical binary format used in spreadsheets.

    If all you are dealing with is data without formatting or commands, you might be better off using comma-separated (CSV) or delimited forms. There are the DBD::CSV and BerkeleyDB that can deal with these kinds of formats.

      Hello .... If I'm using Unix platform .... What can I do ? jahernandezm
Re: Excel from Perl
by trantor (Chaplain) on Aug 07, 2001 at 10:02 UTC

    I suppose this (parsing the entire file, making changes, then writing it back) is the way to go, since modules like DBD::Excel seem to do it.

    I had to face a similar problem some time ago. It was probably easier because I had to edit a set of fixed templates to generate weekly and monthly reports in Excel format for the management.

    Lacking fancy formatting, CSV was not enough <SAD_BUT_TRUE>even though the information was just the same of course</SAD_BUT_TRUE>.

    In the end I decided to parse the templates once, I wrote a set of long-but-easy functions, one for each template, then all I needed was just some additional code to write the actual data into the chosen template.

    If your templates are not complex, you may decide to copy, not parse them, converting by hand into Perl code or whatever suits your needs.

    The additional code I had for each template was a simple (yet longish) function taking a hash as a parameter with all the information I needed to "plug" in the template.

    Obviously this is practical if you always have a fixed set of templates to edit, not sheets that differ every time.

    -- TMTOWTDI

Re: Excel from Perl
by cacharbe (Curate) on Aug 07, 2001 at 16:35 UTC
    What Platform are you on?
    If you are actually on a Win32 Platform, just use Win32::OLE;

    Here's a little example that I wrote for someone else showing how to get the contents of a cell, which, in this case, constitutes a date, and an example of manipulating said date using '::Variant'.

    use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel'; use Win32::OLE::Variant; use Win32::OLE::NLS qw(:LOCALE :DATE); $Win32::OLE::Warn = 2; # Throw Errors, I'll catch them my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit'); my $file = 'c:\perl\projects\excel\variant.xls'; my $Book = $Excel->Workbooks->Open($file); my $Sheet = $Book->Worksheets("Sheet1"); my $dt = Variant(VT_DATE, $Sheet->Range("a1")->{Value}); print "$dt\n"; print $dt->Date(DATE_LONGDATE), "\n"; $Sheet->Range("a1")->{Value} = $dt->Date(DATE_LONGDATE); print $dt->Date("ddd',' MMM dd yy"), "\n"; $Sheet->Range("a1")->{Value} = $dt->Date("ddd',' MMM dd yy"); $Book->ActiveSheet->Save();

    Of course, if you aren't on Win32, the suggestions above hold much more merit than mine.

    Update: I added code for writing out to the excel file, as that was part of the question.

    C-.

Re: Excel from Perl
by Anonymous Monk on Jul 10, 2004 at 06:21 UTC
    I have a similar problem. I am trying to save an excel file as a text file. Since each worksheet need to be saved seperately iam saving the file as below. $Book->Worksheets($i)->SaveAs($filename) now i need to specify the file format for the file that is being saved. MSDN documentation says that the fileFormat should be Excel.XlFileFormat.xlTextWindows but how do i specify this in the SaveAs function is there any way of doing this! any pointers would be greatly helpful pls copy ur response to Swaroop.m@gmail.com Swaroop