http://www.perlmonks.org?node_id=1007790

mellin has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have a problem with the Perl module Excel::Writer::XLSX and certain characters such as Ä and Ö in the finnish Alphabet. They seem to become gibberish in the resulting file. I'm running the Perl script in Centos 6.3 and I've viewed the Excel file both in Windows 7 with Office 2010 and in Centos using LibreOffice Calc. Same problem in both. Any ideas what I might be doing wrong?

I have included a short code example below which produces the same issue in my system.

#!/usr/bin/perl -w use strict; use Excel::Writer::XLSX; my $path = '/tmp'; my $time = time(); my $stamp = $time . '-' . $$; my $filename = "$path/$stamp.xlsx"; # Create a new Excel workbook my $workbook = Excel::Writer::XLSX->new($filename); # Add a worksheet my $worksheet = $workbook->add_worksheet(); # set header format my $format = $workbook->add_format( bold => 1, color => 'black'); $worksheet->write(0, 0, 'Name', $format); $worksheet->write(1, 0, 'Mister Mister'); $worksheet->write(2, 0, 'Mäster Mäster'); $worksheet->autofilter(0, 0, 2, 0); $worksheet->freeze_panes(1, 0);

Replies are listed 'Best First'.
Re: Finnish alphabet and Excel::Writer::XLSX
by Anonymous Monk on Dec 07, 2012 at 18:07 UTC

    I'm not familiar with the module in question, but currently Perl will interpret your strings as latin-1. If your source file is saved as UTF-8, Perl will see "Mäster Mäster"

    If you say use utf8;, your strings will contain the correct character sequence (...assuming you saved the source file in UTF-8. Most *nix editors do that by default today.)

    Furthermore, the Excel::Writer::XLSX perldoc contains a "UNICODE IN EXCEL" section which has a few examples on getting your strings to Perl's internal encoding correctly.

Re: Finnish alphabet and Excel::Writer::XLSX
by rpnoble419 (Pilgrim) on Dec 08, 2012 at 00:02 UTC
    I was bitten by Unicode. Remember if you are getting your data from a database and that data contains Unicode you must create the correct DBI connection to the data. I forgot this with MySQL and could not get the data into the correct format for Excel...