3
###############################################################################
5
# Example of how to use the WriteExcel module
7
# Program to convert a CSV comma-separated value file into an Excel file.
8
# This is more or less an non-op since Excel can read CSV files.
9
# The program uses Text::CSV_XS to parse the CSV.
11
# Usage: csv2xls.pl file.csv newfile.xls
13
# reverse('�'), March 2001, John McNamara, jmcnamara@cpan.org
17
use Spreadsheet::WriteExcel;
20
# Check for valid number of arguments
21
if (($#ARGV < 1) || ($#ARGV > 2)) {
22
die("Usage: csv2xls csvfile.txt newfile.xls\n");
25
# Open the Comma Separated Variable file
26
open (CSVFILE, $ARGV[0]) or die "$ARGV[0]: $!";
28
# Create a new Excel workbook
29
my $workbook = Spreadsheet::WriteExcel->new($ARGV[1]);
30
my $worksheet = $workbook->add_worksheet();
32
# Create a new CSV parsing object
33
my $csv = Text::CSV_XS->new;
35
# Row and column are zero indexed
40
if ($csv->parse($_)) {
41
my @Fld = $csv->fields;
44
foreach my $token (@Fld) {
45
$worksheet->write($row, $col, $token);
51
my $err = $csv->error_input;
52
print "Text::CSV_XS parse() failed on argument: ", $err, "\n";