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;

In reply to XLSX to CSV with high ASCII characters by apu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.