#!perl ############################################################ ## excel2txt-1.0 ## Written By: Rohit Mishra ( rohit[at]rohitmishra.com) ############################################################ use strict; use warnings; use Getopt::Long; use Spreadsheet::ParseExcel; my $VERSION = "1.0"; my $xls_file; my $recordsep; my $help; my $fieldsep; my $txtfile; my $number = 1; GetOptions( "xls=s" => \$xls_file, "recordsep=s" => \$recordsep, "fieldsep=s" => \$fieldsep, "txtfile=s" => \$txtfile, "help" => \$help ); if( $help ){ &Usage; exit(0); } if( ! $xls_file ){ &Usage; print "Null file name argument. Exiting ..\n"; exit(1); } elsif( ! -e $xls_file ){ print "$xls_file: $!\n"; exit(2); } my $excel_obj = Spreadsheet::ParseExcel->new(); my $workbook = $excel_obj->Parse( $xls_file ); die "Workbook did not return worksheets!\n" unless ref $workbook->{Worksheet} eq 'ARRAY'; $fieldsep = $fieldsep || " "; $recordsep = $recordsep || "\n"; open( FILE, ">$txtfile" ) || die( "Could not open file: $txtfile\n" ); for my $worksheet ( @{$workbook->{Worksheet}} ) { print "Writing sheet: $number\n"; my $last_col = $worksheet->{MaxCol}; my $last_row = $worksheet->{MaxRow}; for my $row ( 0 .. $last_row ) { for my $col ( 0 .. $last_col ) { my $cell = $worksheet->{Cells}[$row][$col]; printf FILE ref $cell ? $cell->Value : ''; printf FILE $fieldsep unless $col == $last_col; } print FILE $recordsep; } print FILE "-" x 72,"\n"; $number++; } sub Usage { print < ( Required ) Argument to this option should be an existing, Excel file. -t[xtfile] ( Required ) The output text file generated from the Excel input file. If the Excel file contains more than one worksheet, the script will not generate separate text file for each sheet. Instead it will dump all the text output in a single file, separating the sheet outputs with a line "----" -r[ecordsep] ( Default: "\\n" ) Argument to this option, will be the record separator used, while writing the text file. -f[ieldsep] ( Default: "\\s" ) Argument to this option, will be the field separator used, while writing the text file. -h[elp] Print this help information. EOF } =head1 NAME excel2txt-1.0.pl =head1 DESCRIPTION This script generates the text file from the Microsoft Excel file provided to this script. If the Excel file contains more than one worksheets, the text file will contain all the worksheet contents arranged top to bottom. Usage: excel2txt-1.0.pl -x[ls_file] ( Required ) Argument to this option should be an existing, Excel file. -t[xtfile] ( Required ) The output text file generated from the Excel input file. If the Excel file contains more than one worksheet, the script will not generate separate text file for each sheet. Instead it will dump all the text output in a single file, separating the sheet outputs with a line "----" -r[ecordsep] ( Default: "\\n" ) Argument to this option, will be the record separator used, while writing the text file. -f[ieldsep] ( Default: "\\s" ) Argument to this option, will be the field separator used, while writing the text file. -h[elp] Print this help information. =head1 README This script generates the text file from the Microsoft Excel file provided to this script. If the Excel file contains more than one worksheets, the text file will contain all the worksheet contents arranged top to bottom. =head1 PREREQUISITES This script requires the C module. It also requires C and C. =head1 COREQUISITES Spreadsheet =pod OSNAMES Unix, Solaris, Linux =head1 AUTHOR Rohit Mishra EFE =head1 COPYRIGHT Copyright (c) 2006 Rohit Mishra . All rights reserved This program is a free software; you can redistribute it and/or modify it under the same terms as Perl itself. =pod SCRIPT CATEGORIES Educational/ComputerScience Educational =cut