2012-05-22 27 views
2

我試圖根據cgi.pl example in Excel::Writer::XLSX::Examples向瀏覽器發送OOXML Excel文件。如何避免在CGI環境中使用Excel :: Writer :: XLSX編碼問題?

#!/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, [email protected] 
# 

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__ 

該文件還包含字符,如č š ž ć đ其中顯示像Ä Å¡ Ä Å¾ Ä !。我想這是編碼問題。

有人知道什麼可能是錯的,以及如何解決這個問題?

+0

嘗試將字符集添加到「Content-Type」標頭。它可能是utf-8。所以你會說'print'Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset = utf-8 \ n「;' – simbabque

+0

對不起,已經試過在我的腳本中,但它沒有工作。我遇到了同樣問題的php腳本。我通過使用PHPExcel解決了這個問題,但在這裏,我又遇到了同樣的問題。 :) – TheAptKid

回答

2

你有一個編碼問題。在源代碼中使用Unicode文字時,必須使用utf8編譯指示,並將編碼爲UTF-8的程序源代碼保存起來。這個工程:

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

謝謝,這個工作立即。 – TheAptKid

相關問題