我可以使用 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 因为我想使用颜色。任何人都可以帮忙吗?
我强烈推荐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();
如果您不需要富文本等超级花哨的功能,您可以使用Spreadsheet::WriteExcel,它工作得非常好,而且开销也很低。
编辑:用于my $workbook = Spreadsheet::WriteExcel->new('-');
将您的工作簿直接写入 STDOUT。