<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!perl
############################################################
## txt2excel-1.0
## Written By: Rohit Mishra ( rohit[at]rohitmishra.com)
############################################################

use strict;
use Getopt::Long;
require Spreadsheet::WriteExcel;
require IO::Scalar;

my @file;
my $xls_sheet;
my $help;
my $recordsep;

GetOptions( "file=s" =&gt; \@file,
	    "xls=s" =&gt; \$xls_sheet,
	    "recordsep=s" =&gt; \$recordsep,
	    "help" =&gt; \$help
	  );

@file = split(/,/,join(',',@file ));

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

if( ! @file ){
	&amp;Usage;
	print "Null text file list. Exiting ..\n";
	exit(1);
}
if( ! $xls_sheet ){
	&amp;Usage;
	print "Output file must be specified. Exiting ..\n";
	exit(1);
}

my $number = 1;
my $file = undef;

my $xls_str;
tie *XLS, 'IO::Scalar', \$xls_str;
my $workbook  = Spreadsheet::WriteExcel-&gt;new(\*XLS);
my $name;

foreach $file ( @file ){
	$name = "Category $number";
	print "Reading file: $file\n";
	open F, "&lt;", $file or croak("Can't open $file: $!");
	$/ = $recordsep || "\n";
	my @data = &lt;F&gt;;
	close F;
	$/ = "\n";
	
	print "Generating sheet: $name\n";
	my $worksheet = $workbook-&gt;addworksheet( $name );
	
	for ( my $row = 0; $row &lt; @data; $row++ ) {
		chomp( $data[$row] );
		$data[$row] =~ s/^\s+//;
		my @line = split(/\s+/, $data[$row] );
		for ( my $col = 0; $col &lt; @line; $col++ ) {
			$worksheet-&gt;write_string($row, $col, $line[$col] || "");
		}
	}
	$number++;
}
$workbook-&gt;close();
print "Sheet done.\n";
open( XL_SHEET,"&gt;$xls_sheet" ) || die( "Could not open file: $xls_sheet\n" );
print XL_SHEET $xls_str;
close( XL_SHEET );
print "Output file (XLS): $xls_sheet\n";

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

This script generates the Excel binary from the files provided to this script as input.

Note: The Excel file produced by this script is compatible with Excel 5, 95, 97, 2000
and 2002.

Usage: $0 
	  -f[ile] &lt;comma separated file list&gt; ( Required ) 
	          
		  The argument to this option, can be one or more files.
		  If the argument contains a single file, the output
		  Excel workbook will contain one worksheet.
		  If more than one file is provided to this option, a new 
		  worksheet is added to the workbook, and the elements to 
		  that file, will be added to the new worksheet.

		  The default cell delimiter is hardcoded into this script
		  and is one or more than one space ("\\s" character in perl) 

		  The default record separater can be specified from the command
		  line. ( See -r option below )
	   
	  -x[ls] &lt;O/P xls sheet&gt; ( Required )
		  
		  The argument to this option, will be the output Excel workbook,
		  generated by this script.
		
	  -r[ecordsep] &lt;record separator&gt; ( Default: "\\n" )
		  
		  Argument to this option, will be the record separator used, while
		  reading the files.
	  
          -h[elp]

                 Print this help information.

EOF
}


=head1 NAME

txt2excel-1.0.pl

=head1 DESCRIPTION

This script generates the Microsoft Excel binary file from the files provided to this script as input.

Note: The Excel file produced by this script is compatible with Excel 5, 95, 97, 2000
and 2002.

Usage: txt2excel-1.0.pl -f[ile] &lt;comma separated file list&gt; ( Required ) 
	          
		  The argument to this option, can be one or more files.
		  If the argument contains a single file, the output
		  Excel workbook will contain one worksheet.
		  If more than one file is provided to this option, a new 
		  worksheet is added to the workbook, and the elements to 
		  that file, will be added to the new worksheet.

		  The default cell delimiter is hardcoded into this script
		  and is one or more than one space ("\\s" character in perl) 

		  The default record separater can be specified from the command
		  line. ( See -r option below )
	   
	  -x[ls] &lt;O/P xls sheet&gt; ( Required )
		  
		  The argument to this option, will be the output Excel workbook,
		  generated by this script.
		
	  -r[ecordsep] &lt;record separator&gt; ( Default: "\\n" )
		  
		  Argument to this option, will be the record separator used, while
		  reading the files.
	  
          -h[elp]

                 Print this help information.

=head1 README

This script generates the Microsoft Excel binary file from the files provided to this script as input.
The Microsoft Excel file produced by this script is compatible with MS Excel 5, 95, 97, 2000
and 2002. For details see help on usage.


=head1 PREREQUISITES

This script requires the C&lt;strict&gt; module.  It also requires
C&lt;Spreadsheet::WriteExcel&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>