<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!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" =&gt; \$xls_file,
	    "recordsep=s" =&gt; \$recordsep,
	    "fieldsep=s" =&gt; \$fieldsep,
	    "txtfile=s" =&gt; \$txtfile,
	    "help" =&gt; \$help
	  );

if( $help ){
	&amp;Usage;
	exit(0);
}

if( ! $xls_file ){
	&amp;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-&gt;new();
my $workbook = $excel_obj-&gt;Parse( $xls_file );

die "Workbook did not return worksheets!\n"
unless ref $workbook-&gt;{Worksheet} eq 'ARRAY';

$fieldsep = $fieldsep || " ";
$recordsep = $recordsep || "\n";

open( FILE, "&gt;$txtfile" ) || die( "Could not open file: $txtfile\n" );
for my $worksheet ( @{$workbook-&gt;{Worksheet}} ) {
	print "Writing sheet: $number\n";

	my $last_col = $worksheet-&gt;{MaxCol};
	my $last_row = $worksheet-&gt;{MaxRow};
	
	for my $row ( 0 .. $last_row ) {
		for my $col ( 0 .. $last_col ) {
			my $cell = $worksheet-&gt;{Cells}[$row][$col];
			printf FILE ref $cell ? $cell-&gt;Value : '';
			printf FILE $fieldsep unless $col == $last_col;
		}
		print FILE $recordsep; 
	}
	print FILE "-" x 72,"\n"; 
	$number++;
}

sub Usage {
	print &lt;&lt;EOF;

This script generates the text file from the Excel file provided to this script.

Usage: $0 
	  -x[ls_file] &lt;Excel file&gt; ( Required )
		  
	          Argument to this option should be an existing, Excel file. 

          -t[xtfile] &lt;O/P txt file&gt; ( 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] &lt;record separator&gt; ( Default: "\\n" )

                  Argument to this option, will be the record separator used, while
                  writing the text file.

          -f[ieldsep] &lt;field separator&gt; ( 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] &lt;Excel file&gt; ( Required )
		  
	          Argument to this option should be an existing, Excel file. 

          -t[xtfile] &lt;O/P txt file&gt; ( 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] &lt;record separator&gt; ( Default: "\\n" )

                  Argument to this option, will be the record separator used, while
                  writing the text file.

          -f[ieldsep] &lt;field separator&gt; ( 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&lt;strict&gt; module.  It also requires
C&lt;Spreadsheet::ParseExcel&gt; and C&lt;Getopt::Long&gt;.

=head1 COREQUISITES

Spreadsheet

=pod OSNAMES

Unix, Solaris, Linux

=head1 AUTHOR

Rohit Mishra E&lt;lt&gt;F&lt;rohit@rohitmishra.com&gt;E&lt;gt&gt;

=head1 COPYRIGHT

Copyright (c) 2006 Rohit Mishra &lt;rohit@rohitmishra.com&gt;. 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


</pre></body></html>