Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer


by graff (Chancellor)
on May 29, 2008 at 20:40 UTC ( #689110=sourcecode: print w/replies, xml ) Need Help??
Category: Text Processing
Author/Contact Info graff
Description: A basic, no-frills method to extract data from Excel spreadsheet files and print it as tab-delimited lines to STDOUT (e.g. for redirection to *.txt or *.tsv, or for piping to grep, sort or whatever). Works fine on macosx, linux, unix, once you have Spreadsheet::ParseExcel installed. (updated to fix silly tab indenting, and to remove some cruft, and to make my email address less of a spammer target -- also updated the node title and pod to match the name of the script.) Oh, and I should mention: it does the right thing when cells contain unicode characters: output is utf8.

=head1 NAME



 xls2tsv file.xls > file.tsv


This is a fairly simple-minded tool to extract content from Excel
spreadsheet files and print the cell data in plain-text tab-delimited
format.  The output preserves one or more consecutive empty rows a
single blank line; within non-empty rows, each non-row-final empty
cell is preserved as a tab character.

We loop over the "worksheet" pages in the given .xls file, and output
a ";;; sheetname" line, followed by a blank line, to mark the start of
each one.

When cell data content is found to be unicode, it is printed out in
utf8 encoding.  Leading and trailing spaces are discarded; any cell
found to contain only whitespace characters (including tab,   and
other "wide" space characters available in unicode) is treated as
empty.  Apart from these points, cell contents are output exactly as

Cell and string formatting features are not preserved; changes of
foreground/background color and font style (bold, italic, underline,
etc) are ignored.

=head1 AUTHOR

David Graff  (at


use strict;
use Encode;
use Spreadsheet::ParseExcel;

my $Usage = "$0 file.xls > file.txt\n";
( @ARGV == 1 and -f $ARGV[0] ) or die $Usage;

my $filepath = shift;

my $xl = Spreadsheet::ParseExcel->new;
my $wb = $xl->Parse( $filepath ) or die "$filepath: $!\n";

binmode STDOUT, ":utf8";

for my $sheet ( @{$wb->{Worksheet}} ) {
    printf( ";;; %s\n\n", $sheet->{Name} );
    $sheet->{MaxRow} ||= $sheet->{MinRow};
    my $blankcount = 0;
    for my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) {
        $sheet->{MaxCol} ||= $sheet->{MinCol};
        my $rowtxt = "";
        for my $col ( $sheet->{MinCol} .. $sheet->{MaxCol} ) {
            my $cell = $sheet->{Cells}[$row][$col];
            if ( ! $cell ) {
                $rowtxt .= "\t";
            my $val = $cell->{Val};
            if ( !defined( $val ) or $val eq '' ) {
                $rowtxt .= "\t";
            $val = decode( "UTF-16BE", $val ) if ( $cell->{Code} eq 'u
+cs2' );
            $val =~ s/^\s+//; $val =~ s/\s+$//;
            if ( $val eq '' ) {
                $rowtxt .= "\t";
            $rowtxt .= "$val\t";
        $rowtxt =~ s/\t+$//;
        if ( $rowtxt eq '' ) {
            print "\n" unless ( $blankcount++ );
        else {
            $blankcount = 0;
            print "$rowtxt\n";
    print "\n" unless ( $blankcount );

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://689110]
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (8)
As of 2023-12-06 14:39 GMT
Find Nodes?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?

    Results (30 votes). Check out past polls.