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

Luc*_*uci 0 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";       
Run Code Online (Sandbox Code Playgroud)

#Then循环结果.

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

小智 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();
Run Code Online (Sandbox Code Playgroud)