开发者

Putting UTF-8 characters into Excel with Perl?

开发者 https://www.devze.com 2023-02-11 10:10 出处:网络
I have XML documents that have many characters that are UTF-8 such as \"É\" and \"é\", and I am trying to put these into Excel, but there is something with the encoding that I am just not getting. I

I have XML documents that have many characters that are UTF-8 such as "É" and "é", and I am trying to put these into Excel, but there is something with the encoding that I am just not getting. I am using Win32::OLE to put the data in Excel.

I have tried this:

use Unicode::String qw(utf8 latin1 utf16le);
my $u = utf8($content);
$output = $u->utf16le;

but the only thing that shows in the Excel cells is the first character of the string (correctly encoded). 开发者_Go百科What am I doing wrong here?


You need to enable utf8 coding using Win32::OLE->Option call. The code below works for me (for Eastern-Europe characters):

use utf8;
use Win32::OLE qw(CP_UTF8);

Win32::OLE->Option(CP => CP_UTF8);      # set utf8 encoding

my $excel = Win32::OLE->new('Excel.Application') or die $!;
$excel->{Visible} = 1;

my $wb    = $excel->Workbooks->Add;
my $sheet = $wb->Sheets(1);

$sheet->Range('A1')->{Value} = 'Nějaký český text ďťň';


I seem to have found out why. I simply tried:

$output = $u->latin1;

and that worked perfectly, so I assume that is what encoding Excel uses. The only-showing-first-character part was probably because in utf-16, each character is ended with a null char \0 telling Excel that that's the entire string. (that's just an assumption)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号