如何在CGI环境中避免使用Excel :: Writer :: XLSX编码问题? [英] How do I avoid encoding problems with Excel::Writer::XLSX in a CGI environment?

查看:127
本文介绍了如何在CGI环境中避免使用Excel :: Writer :: XLSX编码问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据 cgi.pl Excel :: Writer :: XLSX ::示例中的示例

I am trying to send an OOXML Excel file to the browser based on the cgi.pl example in Excel::Writer::XLSX::Examples.

#!/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__

该文件还包含像 čšžćđ显示为ÄÅÄžÄ!。我想这是编码的一个问题。

The file also contains characters like č š ž ć đ which are displayed like Ä Å¡ Ä Å¾ Ä !. I guess this is a problem with the encoding.

有人知道可能是什么问题,如何解决这个问题?

Does anybody know what could be wrong and how to fix this?

推荐答案

你有编码问题。当您在源代码中使用Unicode文字时,您必须使用 utf8 pragma,并保存编码为UTF-8的程序源代码。这样做:

You have an encoding problem. When you use Unicode literals in your source code, you must use the utf8 pragma and save your program source code encoded as UTF-8. This works:

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

这篇关于如何在CGI环境中避免使用Excel :: Writer :: XLSX编码问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆