Perl resources, modules, and sample code

Excel spreadsheet to delimited text

Monday, 23 April 2007—subject to ongoing, unnanounced updates

Modern Perl Modern Perl at Powell’s, Modern Perl at Barnes and Noble, Modern Perl at Amazon.com, or download Modern Perl for free and then recommend it to your friends, write a great review, or otherwise spread the word.

Update: Don’t use this code as is. The module—Spreadsheet::ParseExcel—has had its API modernized and the older code, while it works, is harder to debug. See the docs!

use Spreadsheet::ParseExcel;
my $file = shift || die "Give me an Excel file!\n";
-e $file and -r _
    or die "Must provide valid Excel file! $file, $!\n";
my $excel_obj = Spreadsheet::ParseExcel->new();
my $workbook = $excel_obj->Parse($file);
die "Workbook did not return worksheets!\n"
    unless ref $workbook->{Worksheet} eq 'ARRAY';
for my $worksheet ( @{$workbook->{Worksheet}} ) {
    for my $row ( 0 .. $worksheet->{MaxRow} ) {
        for my $col ( 0 .. $worksheet->{MaxCol} ) {
            my $cell = $worksheet->{Cells}[$row][$col];
            print ref $cell ? $cell->Value : '';
            print "\t" unless $col == $worksheet->{MaxCol};
        }
        print "\n"; # record ends
    }
    print "\n";     # worksheet ends
}