<?xml version="1.0" encoding="windows-1252"?>
<node id="72985" title="(code) text2xls.pl (was csv2xls.pl)" created="2001-04-16 22:44:07" updated="2005-08-15 12:41:30">
<type id="1980">
snippet</type>
<author id="14909">
ybiC</author>
<data>
<field name="doctext">
</field>
<field name="snippetdesc">
Convert text file in delimiter-separated format to Excel binary format

&lt;p&gt;
Required switches:
&lt;ul&gt;
  &lt;li&gt;-i or --infile csv_infilename
  &lt;li&gt;-o or --output xls_outfilename
&lt;/ul&gt;

&lt;p&gt;
Optional switches:
&lt;ul&gt;
  &lt;li&gt;-d or --delim alt_delimiter
    &lt;br&gt;default delimiter is ','
  &lt;li&gt;-h or --header
    &lt;br&gt;Accepts no arguments.
    &lt;br&gt;Bolds and background colors contents of topmost row.
&lt;/ul&gt;

&lt;p&gt;
Example:
&lt;br&gt;&lt;tt&gt;text2xls.pl -i file1.csv -o file2.xls -d * -h&lt;/tt&gt;

&lt;p&gt;
&lt;b&gt;Update 2: &lt;/b&gt; Renamed to avoid conflict with existing csv2xls.pl, 
and added Todos re [jmcnamara]'s feedback [73091|below]. &amp;nbsp; 
Hmmm... this started out as a true [Snippet], but I wonder if it should be moved to [Code Catacombs]?

&lt;p&gt;
&lt;b&gt;Update 1: &lt;/b&gt; And if I'd looked just a wee bit further than the pod, I'd have found &lt;tt&gt;/usr/local/lib/site_perl/Spreadsheet/WriteExcel/examples/csv2xls.pl&lt;/tt&gt; which does this exact same thing. &amp;nbsp; Hmmm... I wasn't reinventing a wheel, I was um, learning - yeah, that's it, I was learning! &amp;nbsp; ;^D</field>
<field name="snippetcode">
&lt;CODE&gt;#!/usr/bin/perl -w

# text2xls.pl
# pod at tail

use strict;
use Spreadsheet::WriteExcel;
use Getopt::Long;

# Crank it up
print("\nStarting $0\n");

# Get command line switches and arguments
my %option = ('defaultdelim' =&gt; ',');
GetOptions(
    'infile=s'  =&gt; \$option{csvin},
    'outfile=s' =&gt; \$option{xlsout},
    'delim=s'   =&gt; \$option{delim},
    'header!'   =&gt; \$option{header},
    );
unless (defined ($option{csvin} &amp;&amp; $option{xlsout})) {
    &amp;USAGE();
    exit;
    }
unless (defined $option{delim}) {
    $option{delim} = $option{defaultdelim};
    }
unless (defined $option{header}) {
    $option{header} = 0;
    }

# Do the dirty work
open (CSVFILE, $option{csvin})    or die "Error opening $option{csvin}: $!";
my $workbook  = Spreadsheet::WriteExcel -&gt; new($option{xlsout});
my $worksheet = $workbook -&gt; addworksheet();
my $row       = 0;
while (&lt;CSVFILE&gt;) {
    chomp;
    my @field = split("$option{delim}", $_);
    my $column = 0;
    foreach my $token (@field) {
        $worksheet -&gt; write($row, $column, $token);
        $column++;
        }
    $row++;
    }

# Prettify row as header
if($option{header} == 1) {
    my $header = $workbook -&gt; addformat();
        $header -&gt; set_bold();
        $header -&gt; set_align('center');
        $header -&gt; set_bg_color('tan');
        $header -&gt; set_border();
    $worksheet -&gt; set_row(0, undef, $header);
    }

# Optional, unless doing some external action against xls file
$workbook  -&gt; close()         or die "Error closing $workbook: $!";

# Go tell it on the mountain
print("  infile  = $option{csvin}\n");
print("  outfile = $option{xlsout}\n");
print("Finished $0\n\n");

########################################################
sub USAGE {
print &lt;&lt;EOF

Required switches:
  -i or --infile csv_infilename
  -o or --output xls_outfilename

Optional switches:
-d or --delim alt_delimiter
  default delimiter is ','
-h or --header
  Accepts no arguments.
  Bolds and background colors contents of topmost row.

Example:
  csv2xls.pl -i file1.csv -o file2.xls -d * -h

EOF
;
}
########################################################

=head1 Name

 text2xls.pl

=head1 Description

 Convert text file in csv format to Excel binary format

=head1 Usage

 Required switches:
   -i or --infile csv_infilename
   -o or --output xls_outfilename

 Optional switches:
 -d or --delim alt_delimiter
   default delimiter is ','
 -h or --header
   Accepts no arguments.
   Bolds and background colors contents of topmost row.

 Example:
   text2xls.pl -i file1.csv -o file2.xls -d * -h

=head1 Tested

 Spreadsheet::WriteExcel 0.31
 Getopt::Long            2.19
 Perl                    5.00503
 Debian                  2.2r2 "Espy"
 Excel                   9.0.3821 SR-1
                         97 SR-2
 Win32                   5.0.2195 SP-1
                         4.0 SP-6a

=head1 Updated

 2001-04-17   10:45  Renamed as text2xls - avoid conflict with existing csv2xls.
                     Tweaked todos re feedback from jmcnamara (S::WE author).
 2001-04-16          Initial working code &amp; posted to PerlMonks

=head1 Todos

 Use Text::xSV by tilly or Text::CSV_XS for parsing text infile
 Review and fix "prettify row header" which somewhat works, but not quite right.
   Support for "freeze pane" anticipated in future S::WE
 Test with different delimiters.
 Use Pod::Usage to instead of &amp;USAGE(). But appears to only be in Perl 5.6+ 8^(
 Use @headerparms and foreach loop in headers section to reduce duplication.
 Accept multiple infiles.
 Save as "file.xls" (provided infile is "file") with -r switch.
 Add support for "page delimiter" character for multiple worksheets.
 Add support for $VERSION.

=head1 Author

 ybiC

=head1 Credits

 Thanks to jmcnamara, Petruchio and zdog for suggestions,
 and to jmcnamara for excellent pod in Spreadsheet::WriteExcel,
 and vroom, of course for PM.

=cut

&lt;/CODE&gt;</field>
</data>
</node>
