apu has asked for the wisdom of the Perl Monks concerning the following question:
My input file is an Excel workbook (one worksheet) with names, phone numbers, etc. I'm trying to process this into a CSV file.
xls2csv keeps the high ASCII characters (Nicolás), so I know this is possible, but I need to manipulate the data before generating the CSV (for example: if Preferred Name is provided, use that, otherwise use First Name, or ignore some columns). However, if I process the XLSX file myself, I'm loosing the high ASCII characters.
Comparing my script to xls2csv, I'm at a loss for what I missed. Can a Monk help?
#!/usr/bin/perl use Spreadsheet::Read qw(ReadData); my $inputFile = 'foo.xlsx'; use Text::CSV; my $outputFile = 'bar.csv'; my $csv = Text::CSV->new ( { binary => 1, eol => "\n" } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); my @writeRows; my $book = ReadData ($inputFile); my @readRows = Spreadsheet::Read::rows($book->[1]); foreach my $i (1 .. scalar @readRows) { my @thisRow; my ($id, $last, $first, $pref) = @{$readRows[$i-1]}; if ($pref) { push @thisRow, $pref; } else { push @thisRow, $first; } push @thisRow, $last, "Static", "Text"; push @writeRows, [@thisRow]; } open $fh, ">:encoding(utf-8)", $outputFile or die "$outputFile: $!"; $csv->print ($fh, $_) for @writeRows; close $fh or die "$outputFile: $!"; exit;
|
|---|