I'm a BI developer working with perl scripts as my ETL - I receive data over email, take the file, parse it and push it into the DB. Most of the files are CSV, but occasionally I have an XLSX file.

I've been using Spreadsheet::XLSX to convert, but I've noticed that the CSV output comes out with the wrong encoding (needs to be UTF8, because accents and foreign languages).

That's the sub I'm using ($input_file is an Excel file), but I keep getting the data with the wrong characters.

WHAT am I missing?
Thanks a lot all!

sub convert_to_csv { my $input_file = $_[0]; my ( $filename, $extension ) = split( '\.', $input_file ); open( format_file, ">:**encoding(utf-8)**", "$filename.csv" ) or d +ie "could not open out file $!\n"; my $excel = Spreadsheet::XLSX->new($input_file); my $line; foreach my $sheet ( @{ $excel->{Worksheet} } ) { #printf( "Sheet: %s\n", $sheet->{Name} ); $sheet->{MaxRow} ||= $sheet->{MinRow}; foreach my $row ( $sheet->{MinRow} .. $sheet->{MaxRow} ) { $sheet->{MaxCol} ||= $sheet->{MinCol}; foreach my $col ( $sheet->{MinCol} .. $sheet->{MaxCol} ) { my $cell = $sheet->{Cells}[$row][$col]; if ($cell) { my $trimcell; $trimcell = $cell->value(); print STDERR "cell: $trimcell\n"; ## Just for the +tests so I don't have to open the file to see if it's ok $trimcell =~ s/^\s+|\s+$//g; ## Just to make sure + I don't have extra spaces $line .= "\"" . $trimcell . "\","; } } chomp($line); if ($line =~ /Grand Total/){} ##customized for the files else { print format_file "$line\n"; $line = ''; } } } close format_file; }

In reply to Converting XLSX to CSV with Perl while maintaining the encoding by AALB

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.