Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
#!/usr/bin/perl =head1 NAME xls2tsv =head1 SYNOPSIS xls2tsv file.xls > file.tsv =head1 DESCRIPTION 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 found. 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 ldc.upenn.edu) =cut 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"; next; } my $val = $cell->{Val}; if ( !defined( $val ) or $val eq '' ) { $rowtxt .= "\t"; next; } $val = decode( "UTF-16BE", $val ) if ( $cell->{Code} eq 'u +cs2' ); $val =~ s/^\s+//; $val =~ s/\s+$//; if ( $val eq '' ) { $rowtxt .= "\t"; next; } $rowtxt .= "$val\t"; } $rowtxt =~ s/\t+$//; if ( $rowtxt eq '' ) { print "\n" unless ( $blankcount++ ); } else { $blankcount = 0; print "$rowtxt\n"; } } print "\n" unless ( $blankcount ); }

In reply to xls2tsv by graff

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (9)
    As of 2014-09-22 15:10 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      How do you remember the number of days in each month?











      Results (198 votes), past polls