如何使用perl在excel xls文件上保存数据?

时间:2009-12-02 15:55:56

标签: excel xls

我可以使用perl将excel文件保存为.csv,如下所示:

print "Content-type: application/vnd.ms-excel\n";
print "Content-Disposition: attachment;filename=\"file name.xls\"\n\n";    
print"Fruits, Cost";       

#Then循环结果。

然而我需要将其保存为.xls因为我想使用颜色。任何人都可以帮忙吗?

3 个答案:

答案 0 :(得分:7)

我强烈建议Spreadsheet::WriteExcel满足您的需求。该库完全用Perl编写,因此您只需将CPAN库上传到您的网站并指定具体位置即可。下面的库文档和代码片段可以帮助您入门。

#!/usr/bin/perl -w
use strict;
use lib qw(./lib);   # Place for the downloaded WriteExcel library
use Spreadsheet::WriteExcel;

# Send headers
print "Content-type: application/vnd.ms-excel\n";
print "Content-disposition: attachment;filename=rollcharts.org.xls\n\n";

# Create a new workbook and add a worksheet
my $workbook  = Spreadsheet::WriteExcel->new("-");
my $worksheet = $workbook->add_worksheet("Colorful Example");

# Create a new format with red colored text
my $format = $workbook->add_format();
$format->set_color('red');

# Add header    
$worksheet->write(0, 0, "Fruit.", $format);
$worksheet->write(0, 1, "Cost", $format);

# Add Data
$worksheet->write(1, 0, "Apple");
$worksheet->write(1, 1, "10.25");

# Close Workbook
$workbook->close();

答案 1 :(得分:3)

如果你不需要像富文本那样的超级功能,你可以使用Spreadsheet::WriteExcel,它工作得非常好,而且开销也很低。

编辑:使用my $workbook = Spreadsheet::WriteExcel->new('-');将您的工作簿直接写入STDOUT。

答案 2 :(得分:0)