如何使用Perl将数据保存在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"; 

#然后循环的结果。

但是我需要保存这个.xls因为我想要使用颜色。 任何人都可以帮忙?

我强烈build议您的需要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(); 

如果你不需要超富有的function,比如丰富的文本,你可以使用Spreadsheet :: WriteExcel ,它工作的很好,而且开销也很低。

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