#!/usr/bin/perl;
use strict;
use warnings;
use Text::CSV_XS;
use Spreadsheet::WriteExcel;
my $row = 0;
my $infile = $ARGV[0];
open (my $fh, $infile) or die "$infile: $!";
my $outfile = "$infile";
if ($infile =~ /\.csv/) {
$outfile =~ s/\.csv/.xls/;
}
else {
$outfile = $infile . ".xls";
}
my $workbook = Spreadsheet::WriteExcel->;new($outfile);
my $worksheet = $workbook->;add_worksheet("sheet1");
while (my $rowdata = $csv->getline ($fh)) {
my @rowdata = @$rowdata;
foreach my $col (0 .. $#rowdata) {
my $field = $rowdata[$col] || "";
$worksheet->write($row, $col, $field);
}
$row++;
}
close $fh or die "$fh: $!";
Note the'allow_whitespace' parameter on the output file open. This is one that's driven a lot of folks nuts. Strict CSV grammar says that there is no whitespace in between delimiters, i.e.
"testing",1,2,3,"four","five",6
However, if you're writing out numbers with a trailing sign (did I mention I'm a COBOL programmer?), you're going to get a space there with positive numbers:
"testing",1 ,2 , etc.
Excel will see these as 'Number stored as text' and give you an error, and refuse to format them nicely. The 'allow_whitespace' parameter eliminates this.
No comments:
Post a Comment