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

Hi All, Is it possible to output information from a PERL script to an Excel spreadsheet? If so is there a specific way to go about it?

Replies are listed 'Best First'.
Re: Output to an Excel Document
by adrianh (Chancellor) on Mar 12, 2003 at 12:24 UTC
Re: Output to an Excel Document
by OM_Zen (Scribe) on Mar 12, 2003 at 15:01 UTC
    Hi ,

    The xls spreadsheets can be generated using the distribution Spreadsheet::WriteExcel like as in here

    use Spreadsheet::WriteExcel; # open the text one in here my $Files = shift; my $FilesExcel =$Files . "\.xls"; $FilesExcel =~ s/\.txt//g; my $WorkbookForFiles = Spreadsheet::WriteExcel->new($FilesExcel); my $WorksheetForFiles = $WorkbookForFiles->addworksheet('FTP_Outg +oing_Alert_Tracking_ Report'); # Change the permission to 770 however you have to my $format_for_WorkbookFiles= $WorkbookForFiles->addformat(); $format_for_WorkbookFiles->set_bold(1); $format_for_WorkbookFiles->set_color('black'); $format_for_WorkbookFiles->set_align('center'); $WorksheetForFiles->set_column(0,20,20); open(fh , "<$Files"); my $rownum = 0; my @fields; my $columnum; while (<fh>){ $columnnum = 0; chomp; @fields = split('\|',$_); $WorksheetForFiles->activate(); foreach my $Fields(@fields){ $WorksheetForFiles->write($rownum ,$columnnum,$Fields,$for +mat_for_WorkbookFile s); $columnnum++; } $rownum++; } }


Re: Output to an Excel Document
by nite_man (Deacon) on Mar 12, 2003 at 12:37 UTC
    You can try to use DBD::Excel module and Spreadsheet family of modules.
    Note that CPAN is your the best friend, except for Perl Monks ;-))
    ---> SV* sv_bless(SV* sv, HV* stash);
Re: Output to an Excel Document
by derby (Abbot) on Mar 12, 2003 at 12:42 UTC
    If your spreadsheet is quite simple, you could just output it as a tab delimitted file with a suffix Excel knows about (xsl?). Excel does a very good job of auto-magically converting tab delimitted files.

    -derby

      I dunno about xls, but I know that csv will work.

      thor

      Agreed, just be careful if fields starting with a double quote which is not matched later on. This starts an ad-hoc interpretation of the rest of the file as CSV...

      I was bitten by this just recently. Of course a simple regex will help in this case (pseudo code):

      for each $field { $field =~ s/^"//; }

      --
      Cheers, Joe

Re: Output to an Excel Document
by meetraz (Hermit) on Mar 12, 2003 at 16:30 UTC
    Also, if you are generating Excel documents on windows, and you have Excel installed, you can use Win32::OLE to create actual Excel documents. This has the advantage of letting you work with native documents, apply formatting, generate graphs/charts, etc. Basically anything you could do actually working in Excel.

    Just fire up perldoc win32::ole for an example.

Re: Output to an Excel Document
by pg (Canon) on Mar 12, 2003 at 15:46 UTC
    You may also want to check Win32::ODBC, which is a core module comes with the standard AS Perl distribution.