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

Is there a way in Perl that i can access / create an excel file ?

Replies are listed 'Best First'.
Re: Can I Create/Access an excel file ?
by adrianh (Chancellor) on Apr 27, 2004 at 11:02 UTC

    Take a look at the various Spreadsheet::Excel modules on CPAN.

    (and it's Perl or perl not PERL)

      But before you do that, do you need to read/write an excel file or could you just use a comma separated file? If all you need is to write a file excel can open then you could just write a csv. If you don't need anything fancy ( formula , coloured backgrounds etc ) then this may be a better idea.
Re: Can I Create/Access an excel file ?
by dragonchild (Archbishop) on Apr 27, 2004 at 11:39 UTC
    The primary way to read binary Excel files is to use Spreadsheet::ParseExcel. The standard module to write binary Excel files is Spreadsheet::WriteExcel. There is a templating module Excel::Template that can help writing excel files.

    Excel can also read XML and HTML tables. You'll have to review the Excel documentation for more info.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

Re: Can I Create/Access an excel file ?
by rupesh (Hermit) on Apr 27, 2004 at 12:44 UTC
    You can do the same and (probably) a little bit more with Win32::OLE.
    Check it out...
Re: Can I Create/Access an excel file ?
by perlinux (Deacon) on Apr 27, 2004 at 12:57 UTC
    There's a Spreadsheet::WriteExcel on CPAN. It's very esay to use, to create worksheets and format dates, string, etc... Look at the dimension of your output files, I remember a problem on this... Try it!
Re: Can I Create/Access an excel file ?
by Art_XIV (Hermit) on Apr 27, 2004 at 13:06 UTC

    A CPAN search can reveal many Perl/Excel mysteries.

    Hanlon's Razor - "Never attribute to malice that which can be adequately explained by stupidity"
Re: Can I Create/Access an excel file ?
by raptnor2 (Beadle) on Apr 28, 2004 at 00:52 UTC
    Yep, and it's really easy. Create the document as html and save it with an *.xls extention. I do it all of the time.

    Cheers,

    John

Re: Can I Create/Access an excel file ?
by PhilHibbs (Hermit) on Apr 28, 2004 at 15:40 UTC
    Here's a Batch/Perl polyglot that reads the first sheet of a spreadsheet, expands the data, and writes it to the fourth sheet:
    @rem = '--*-Perl-*-- @echo off if exist C:\Perl\bin\perl.exe goto ok if exist D:\Perl\bin\perl.exe goto ok if exist E:\Perl\bin\perl.exe goto ok echo Perl not properly installed goto endofperl :ok perl "%~dpnx0" %* goto endofperl @rem '; #!perl #line 14 # use strict; # use warnings; use Win32::OLE; # use existing instance if Excel is already running eval {$ex = Win32::OLE->GetActiveObject('Excel.Application')}; die "Excel not installed" if $@; unless (defined $ex) { $ex = Win32::OLE->new('Excel.Application', sub {$_[0]->Quit;}) or die "Oops, cannot start Excel"; } my $file = shift; my $book = $ex->Workbooks->Open($file) or die "Can't open $file"; my $source = $book->Worksheets(1) or die "Can't access first sheet"; my $target = $book->Worksheets(4) or die "Can't access fourth sheet"; my $startrow = 3; my $stoprow = 199; my $array = $source->Range("A$startrow:Q$stoprow")->{Value}; my $writerow = $startrow; for (@$array) { my @vals = (); for (@$_) { push @vals,$_ } next unless defined($vals[0]); my @bitlist = (); for (my $pos = 0; $pos < scalar @vals; ++$pos) { push @bitlist,$pos if $vals[$pos] eq '>=0'; } my $max_val = 2 ** (scalar @bitlist); for my $val ( 0..$max_val-1 ) { my @newvals = @vals; for my $bit (@bitlist) { $newvals[$bit] = ($val/2 == $val>>1) ? 1 : 0; $val >>= 1; } for ( @newvals ) { s/>0/1/; # >0 becomes 1 s/'?([0-9]+)/'$1/; # Make sure ' is present for numbers } $target->Range("A$writerow:Q$writerow")->{Value} = [@newvals]; for (@newvals) { s/^'//; } my @single = ($newvals[3].$newvals[4].$newvals[5].$newvals[6]. $newvals[7].$newvals[8].$newvals[9].$newvals[10]. $newvals[11].$newvals[12].$newvals[13].$newvals[14]) +; $target->Range("C$writerow:C$writerow")->{Value} = [@single]; ++$writerow; print join("\t",@newvals)."\n"; } } # save and exit $book->Save(); $book->Close(); undef $book; undef $ex; __END__ :endofperl