如何在CGI环境中避免使用Excel :: Writer :: XLSX编码问题?

时间:2012-05-22 08:07:38

标签: perl encoding

我正在尝试根据cgi.pl example in Excel::Writer::XLSX::Examples向浏览器发送OOXML Excel文件。

#!/usr/bin/perl

###############################################################################
#
# Example of how to use the Excel::Writer::XLSX module to send an Excel
# file to a browser in a CGI program.
#
# On Windows the hash-bang line should be something like:
#
#     #!C:\Perl\bin\perl.exe
#
# The "Content-Disposition" line will cause a prompt to be generated to save
# the file. If you want to stream the file to the browser instead, comment out
# that line as shown below.
#
# reverse('©'), March 2001, John McNamara, jmcnamara@cpan.org
#

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

# Set the filename and send the content type
my $filename = "cgitest.xlsx";

print "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet\n";

# The Content-Disposition will generate a prompt to save the file. If you want
# to stream the file to the browser, comment out the following line.
print "Content-Disposition: attachment; filename=$filename\n";
print "\n";

# Redirect the output to STDOUT. Binmode the filehandle in case it is needed.
binmode STDOUT;

my $workbook  = Excel::Writer::XLSX->new( \*STDOUT );
my $worksheet = $workbook->add_worksheet();


# Set the column width for column 1
$worksheet->set_column( 0, 0, 20 );


# Create a format
my $format = $workbook->add_format();
$format->set_bold();
$format->set_size( 15 );
$format->set_color( 'blue' );


# Write to the workbook
$worksheet->write( 0, 0, "Hi Excel!", $format );

__END__

该文件还包含č š ž ć đ等字符,显示为Ä Å¡ Ä Å¾ Ä !。我想这是编码的问题。

有人知道可能出现的问题以及如何解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您遇到编码问题。在源代码中使用Unicode文字时,必须使用utf8编译指示并保存编码为UTF-8的程序源代码。这有效:

use utf8;
⋮
$worksheet->write( 0, 0, "Hi Excel! č š ž ć đ", $format );