Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Excel To Tab Delimited using Spreadsheet::ParseExcel

by upallnight (Sexton)
on Mar 23, 2010 at 18:44 UTC ( [id://830364]=CUFP: print w/replies, xml ) Need Help??

Run from command line. Simply parses an excel file and saves it as a tab-delimited file.

#!/usr/local/bin/perl ###################### # Author: Alan Hamlett # Date: 03/18/2009 ###################### use strict; use Spreadsheet::ParseExcel; my $inputFile = shift; my $outputFile = shift; if(!-e $inputFile || !$outputFile) { print qq~ Usage: excelToTabDelim inputFile outputFile Parses an excel input file into a tab delimited out file ~; exit 1; } if(open(OUT, "> $outputFile")) { my $workbook = Spreadsheet::ParseExcel::Workbook->Parse($inputFile +); foreach my $worksheet (@{$workbook->{Worksheet}}) { # looping thro +ugh worksheets for(my $row = $worksheet->{MinRow}; defined $worksheet->{MaxRo +w} && $row <= $worksheet->{MaxRow}; $row++) { # loop through rows my @line; for(my $column = $worksheet->{MinCol}; defined $worksheet- +>{MaxCol} && $column <= $worksheet->{MaxCol}; $column++) { # loop thr +ough columns my $value = $worksheet->{Cells}[$row][$column] ? $work +sheet->{Cells}[$row][$column]->Value : ""; $value =~ s/^\s+//; $value =~ s/\s+$//; $value =~ s/[\r\n]+//g; $value = " " if $value ne "0" && !$value; push(@line, $value); } $" = "\t"; print OUT "@line\n"; } } close(OUT); } else { print "Error: Could not write to output file '$outputFile'\n"; }

Replies are listed 'Best First'.
Re: Excel To Tab Delimited using Spreadsheet::ParseExcel
by toolic (Bishop) on Mar 23, 2010 at 18:58 UTC
    See also xls2csv on CPAN which outputs a CSV file (commas instead of tabs).
      good work buddy....i am going to try this
      How can I retain empty cells? My report starts on the 6th column. When I use this, it removes the empty cells. I would want to have 5 tabs representing the first 5 empty columns.
        A similar script that I posted here a couple years prior to the one above preserves empty cells that occur to the left of non-empty cells. (It also handles unicode character content, in case that's relevant.) Here: xls2tsv
        use the option that is documented to stop doing that :) (hint, read the "f"riendly docs)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: CUFP [id://830364]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (3)
As of 2024-04-19 20:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found