0% found this document useful (0 votes)
27 views2 pages

CSV

This Perl script parses Excel files by reading worksheets and cells and outputting data as delimited text. It takes an Excel file as input, parses the data, and prints it to standard output with customizable field and record delimiters.

Uploaded by

Anil Karunakaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views2 pages

CSV

This Perl script parses Excel files by reading worksheets and cells and outputting data as delimited text. It takes an Excel file as input, parses the data, and prints it to standard output with customizable field and record delimiters.

Uploaded by

Anil Karunakaran
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#!

/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;
}
#===========================================================

You might also like