0

I try to create an Excel-file with perl and Excel::Writer::XLSX with an Excel-name which contains umlauts, but do not succeed.

use strict;
use warnings;
use Excel::Writer::XLSX;

my $excel_name = "äöü.xlsx";
print "\n$excel_name\n";

my $workbook  = Excel::Writer::XLSX->new( $excel_name );
$workbook->close;

The Excel-file I got is named: äöü.xlsx. Also if I print the name on STDOUT the output has strange symbols: ├â┬ñ├â┬Â├â┬╝.xlsx.

When I print the name in a text file:

make_excel.pl >test.txt

the text-file contains the correct output äöü.xlsx and has utf-8 encoding (determined with Notepad++).

The working environment is, Windows 7, active codepage is cp950 and perl version 5.28 (Strawberry).

I tried using (among a lot of others):

use open ':std', ':encoding(cp850)';

but it does not gives the desired output.

Does anyone have an idea how I can get an Excel-file with umlaut?

Update 1: The code is UTF-8 (according to Notepad++ and Emacs C-h v buffer-file-coding-system RET)

giordano
  • 2,639
  • 4
  • 31
  • 55
  • I'm not sure if this will work, but try encoding the output as `utf16-le` which is Windows native encoding – JGNI Dec 10 '18 at 14:29
  • 1
    Perl asumes that source code is Latin-1. So the string "äöü.xlsx" is interpreted as Latin-1. – ceving Dec 10 '18 at 14:29
  • Possible duplicate of [How can I output UTF-8 from Perl?](https://stackoverflow.com/questions/627661/how-can-i-output-utf-8-from-perl) – ceving Dec 10 '18 at 14:32
  • I add `use open ':std', ':encoding(utf16-le)';`and got `├ ñ ├ Â ├ ╝ . x l s x`. – giordano Dec 10 '18 at 14:33
  • That affects the content not the name of the file. See here: https://perldoc.perl.org/utf8.html – ceving Dec 10 '18 at 14:34
  • Also `binmode(STDOUT, ":utf8");` does not fix the problem. – giordano Dec 10 '18 at 14:43
  • `use utf8; $excel_name = encode('latin1',$excel_name);` does not fix the problem. Tried also with: latin-1, utf16-le. – giordano Dec 10 '18 at 14:55
  • 2
    The Windows APIs that Perl uses assume Latin-1 filename encodings,while parts of the world think that filenames must represent UTF-8 octets and other parts think you need to use the appropriate Win32 `*W` APIs to get at the properly encoded filenames. Your script is bad because likely your script was saved as `UTF-8` but you do not specify the encoding of the script in the source code. If you really want to create filenames outside the 7-bit ASCII range on Windows, see https://www.perlmonks.org/?node_id=1162804 , https://metacpan.org/pod/Win32::File and https://metacpan.org/pod/Win32::LongPath – Corion Dec 10 '18 at 15:59
  • @Corion Thanks for these informations. At the end I found the solution [here](http://perl.mines-albi.fr/perl5.8.5/5.8.5/utf8.html): `use utf8; decode($excel_name);`. This gives me the desired Excel-file. The output on the console remains cryptic. – giordano Dec 10 '18 at 17:31
  • 1
    A lot of Win32 perl modules do not support unicode in file names because they use ANSI Windows API. But in your case, package should use COM to work with Excel and Windows API uses utf16-le for unicode support as was mentioned above, so try to pass file name encoded in utf16-le – Kostia Shiian Dec 10 '18 at 18:15

0 Answers0