我正在读取Excel中的数据,如果单元格中的文本包含umlauts (äöü),我的Perl脚本就不能正确地看到它们。该字符被替换为替换字符。
我需要做什么才能正确读取Excel中的特殊字符?
# get reference to Excel, Active Window, Active Sheet
my $excel = Win32::OLE->GetActiveObject('Excel.Application');
my $book = $excel -> ActiveWindow;
my $sheet = $book -> ActiveSheet();
my $text = $sheet->Cells(1, 2)->{Value};
当打印内容到 Windows 命令提示符窗口并使用 STDOUT 编码时,对我来说是可行的 (Windows 10, Strawberry Perl 5.30)。cp437
:
use feature qw(say);
use strict;
use warnings;
use Win32::OLE;
use open ':std', ':encoding(cp437)';
# get reference to Excel, Active Window, Active Sheet
my $excel = Win32::OLE->GetActiveObject('Excel.Application');
my $book = $excel -> ActiveWindow;
my $sheet = $book -> ActiveSheet();
my $text = $sheet->Cells(1, 1)->{Value};
say $text;
产出:
äöü
编辑:
正如 @ikegami 所指出的,你应该用编程的方式来确定控制台的输出代码页(而不是硬编码的值)。cp437
如我)这样。
use Win32;
my $coe = "cp" . Win32::GetConsoleOutputCP();
binmode STDOUT, "encoding($coe)";
又见 这个 更多信息的帖子。