views:

2310

answers:

4

What's the simplest way to parse an Excel file in Perl? Converting it to a text file would also work.

+6  A: 

I have had great luck using Spreadsheet::ParseExcel.

Mr. Muskrat
I've good luck with the Ruby port of it as well
Matt Ephraim
+3  A: 

The 'best' way would ideally be to use a module from our beloved CPAN.

Whenever you have a problem Instantly ask: Why have I not checked CPAN yet?

Which module however I can't be certain, here is a list to get you started, try them, see what works.

Kent Fredric
+12  A: 

The best way is to use Spreadhsheet::ParseExcel.

Here is an example:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;

my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->Parse('Book1.xls');

for my $worksheet ( $workbook->worksheets() ) {

    my ( $row_min, $row_max ) = $worksheet->row_range();
    my ( $col_min, $col_max ) = $worksheet->col_range();

    for my $row ( $row_min .. $row_max ) {
        for my $col ( $col_min .. $col_max ) {

            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            print "Row, Col    = ($row, $col)\n";
            print "Value       = ", $cell->value(),       "\n";
            print "Unformatted = ", $cell->unformatted(), "\n";
            print "\n";
        }
    }
}

To convert an Excel file to text with Perl I'd recommend excel2txt which uses Spreadsheet::ParseExcel.

jmcnamara
+1 for having an example.
cletus