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

I've got no problems using Spreadsheet::WriteExcel it's a easy, wonderful module. However, it cannot write to an already exisiting Excel file. So I installed Spreadsheet::ParseExcel however I, for the life of me, do not understand how can I open up an existing .xls and then modify it. I did read Spreadsheet::ParseExcel's docs, however I just don't understand them, he doesn't provide an example on how to do this. Here's my current code.. Please help.
#!/usr/bin/perl use Spreadsheet::WriteExcel; use Spreadsheet::ParseExcel; my $oExcel = new Spreadsheet::ParseExcel; my $weekday = (Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturda +y)[(localtime)[6]-1] ; my $dayofmonth = (localtime)[3] - 1; my $month = (January, February, March, April, May, June, July, August, + September, October, November, December)[(localtime)[4]]; my $date = (localtime)[3] - 1; my $column = (localtime)[6] + 1; my $row = 2 + $dayofmonth; my $excelrow = $row + 1; my $aolstats; my $msnstats; my $wwwstats; my $aolfile = "/stats/aol"; my $msnfile = "/stats/msn"; my $wwwfile = "/stats/www"; ######################### # Get the stats ######################### open(AOLFILE, $aolfile) or die "Couldn't open AOL stats: $!\n"; $aolstats = <AOLFILE>; if ($aolstats =~ /\d{4}-\d{2}-\d{2}-aol\s+(.*)/) { $aolstats = $1; } open(MSNFILE, $msnfile) or die "Couldn't open MSN stats: $!\n"; $msnstats = <MSNFILE>; if ($msnstats =~ /\d{4}-\d{2}-\d{2}-msn\s+(.*)/) { $msnstats = $1; } open(WWWFILE, $wwwfile) or die "Couldn't open WWW stats: $!\n"; $wwwstats = <WWWFILE>; if ($wwwstats =~ /\d{4}-\d{2}-\d{2}-www\s+(.*)/) { $wwwstats = $1; } ################################ # Get rid of the comma in stats ################################ if ($aolstats =~ /(\d+),(\d+)/) { $aolstats = $1 . $2; } if ($msnstats =~ /(\d+),(\d+)/) { $msnstats = $1 . $2; } if ($wwwstats =~ /(\d+),(\d+)/) { $wwwstats = $1 . $2; } ################################ # Create the worksheets ################################ my $workbook = $oExcel->Parse('test2.xls'); #this is what i tried, doe +snt work #my $workbook = Spreadsheet::WriteExcel->new("test2.xls"); my $worksheet1 = $workbook->addworksheet('1st Quarter 2002'); my $worksheet2 = $workbook->addworksheet('2nd Quarter 2002'); my $worksheet3 = $workbook->addworksheet('3rd Quarter 2002'); my $worksheet4 = $workbook->addworksheet('4th Quarter 2002'); my $worksheet5 = $workbook->addworksheet('Daily Traffic 4th Quarter'); $worksheet3->activate(); # Worksheet that you see when you open up t +ext2.xls my $format = $workbook->addformat(); $format->set_bold(); $worksheet3->write(0, 0, "KATRILLION TRAFFIC 3rd QUARTER JULY-AUGUST-S +EPTEMBER", $format); $worksheet3->write(2, 0, 'Date', $format); $worksheet3->write(2, 1, 'Day', $format); $worksheet3->write(2, 2, 'AOL', $format); $worksheet3->write(2, 3, 'MSN', $format); $worksheet3->write(2, 4, 'Katrillion', $format); $worksheet3->write(2, 5, 'CS', $format); $worksheet3->write(2, 6, 'Daily Total', $format); $worksheet3->write(2, 7, 'AOL Weekly', $format); $worksheet3->write(2, 8, 'Mo. Run Total', $format); $worksheet3->write(2, 9, '% change', $format); $worksheet3->write($row, 0, "$month $date"); $worksheet3->write($row, 1, $weekday); $worksheet3->write($row, 2, $aolstats); $worksheet3->write($row, 3, $msnstats); $worksheet3->write($row, 4, $wwwstats); $worksheet3->write_formula($row, 6, "=SUM(C$excelrow:F$excelrow)" );

Replies are listed 'Best First'.
Re: Need help with opening up a Excel file then modifying it.
by jmcnamara (Monsignor) on Aug 21, 2002 at 14:18 UTC

    The best thing to do is to use the Spreadsheet::ParseExcel::SaveParser module which is part of the Spreadsheet::ParseExcel distribution.

    SaveParser wraps Spreadsheet::ParseExcel and Spreadsheet::WriteExcel into one module so that you can read an existing file and then rewrite it. Here is a modified example from the docs using the new simplified interface:

    $workbook = Spreadsheet::ParseExcel::SaveParser::Workbook->Parse('te +st.xls'); my $worksheet = $workbook->AddWorksheet('New Sheet'); $worksheet->AddCell(10, 1, 'New Cell'); $workbook->SaveAs('iftest.xls');

    Unfortunately, the worksheet object returned by SaveParser isn't a WriteExcel worksheet object. Therefore, you will have to use the SaveParser methods to add formatting.

    Also, formulas that are written by WriteExcel aren't read by ParseExcel for the reasons described here. I proposed a solution for this to the author of ParseExcel but he hasn't responded.

    Just try to read the ParseExcel docs carefully and study the examples.

    All in all the interface between these two modules should be cleaner. :-(

    --
    John.

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: Need help with opening up a Excel file then modifying it.
by Mr. Muskrat (Canon) on Aug 21, 2002 at 15:22 UTC

    I was trying to do something similar recently. I tried several different approaches and received some guidance from jmcnamara. In the end, I went with the following because I already has most of the code in place.

    I simply read the spreadsheet line by line and write it to a new one formatting as I go. (I tried to read the formats and apply them to the data but it wasn't working as expected so I will need to further investigate that.)

    If I were starting fresh, I would use the Spreadsheet::ParseExcel::SaveParser module like jmcnamara recommends.

    As far as the formulas go, you already know what they are, so it should be a piece of cake to put them back in the right places.

        I'm using this on Linux, so Win32 modules are a bit useless to me, unless i'm missing something..? but i already got this script to work anyway (open up an existing .xls and modifying it) thanks anyway =)
Re: Need help with opening up a Excel file then modifying it.
by ides (Deacon) on Aug 21, 2002 at 14:07 UTC

    I believe what you need to do is use Spreadsheet::ParseExcel to gather the data from the existing Excel file and then use Spreadsheet::WriteExcel to build an Excel file with the original data and your changes.

    -----------------------------------
    Frank Wiles <frank@wiles.org>
    http://frank.wiles.org
    A reply falls below the community's threshold of quality. You may see it by logging in.