Converting Excel to delimited text
Social links
View Ashley Pond V's profile on LinkedIn
Miscellaneous

Other pages

Description

This is something I put together because I was asked about it and it only makes sense to have it here. Sorry there isn’t more information included. Please see this page for more about the reverse process and available modules to help both.

Below is a simple script/subroutine that can take an Excel file as an argument and print its contents in delimited plain text to standard output.

Code
#!/usr/bin/perl 
use strict;
use warnings;

#  Declarations
#===========================================================
use Spreadsheet::ParseExcel;

#  Program proper
#===========================================================

my $file_name = shift || usage();

parse_excel($file_name);

exit 0;

#  Subroutines
#===========================================================
sub parse_excel {

    my %arg = ( record_sep => "\n",
                field_sep  => "\t",
                xls        => undef );

    if ( @_ == 1 ) {
        $arg{xls} = shift;
    } elsif ( not @_ % 2 ) {
        %arg = ( %arg, @_ );
    }

    -e $arg{xls} or 
        die "Must provide valid XLS file! $arg{xls}, $!\n";

    # create a ParseExcel object
    my $excel_obj = Spreadsheet::ParseExcel->new();

    my $workbook = $excel_obj->Parse($arg{xls});

    # make sure we're in business
    die "Workbook did not return worksheets!\n"
        unless ref $workbook->{Worksheet} eq 'ARRAY';

    # we need to get each worksheet from the workbook in turn

    for my $worksheet ( @{$workbook->{Worksheet}} ) {

    # {Cells}[row][col]
    # empty worksheets have undef for MaxCol and MaxRow
        my $last_col = $worksheet->{MaxCol} || 0;
        my $last_row = $worksheet->{MaxRow} || 0;

        for my $row ( 0 .. $last_row ) {

            for my $col ( 0 .. $last_col ) {

                my $cell = $worksheet->{Cells}[$row][$col];

                print ref $cell ?
                    $cell->Value : '';

                print $arg{field_sep} unless $col == $last_col;
            }
            print $arg{record_sep}; # record ends
        }
        print "\n"; # worksheet ends
    }
}
#===========================================================
sub usage {

    my ( $tool ) = $0 =~ m,([^\/]+$),;

    print <<HERE;
------------------------------------------------------------
USAGE:

   $tool EXCEL_FILE.xls [field_delim] [record_delim]

Takes an Excel file, parses it into plain text delimited 
fields and rows, and sends the results to STDOUT. The default 
field and record delimiters are "\\t" and "\\n" if neither 
is given.
------------------------------------------------------------
HERE
    exit 0;
}
#===========================================================

Description

Buglet fix for undefined MaxCol and MaxRow suggested by Dr Timur Shtatland.

Search these pages via Google
Text, original code, fonts, and graphics ©1990-2008 Ashley Pond V.