How to save data on excel xls file using perl?

2019-09-13 18:25发布

I'm able to save the excel file as .csv using perl like this :

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

#Then looping on the results.

Yet I need to save this as .xls cause I want to use colours. Any one can help?

标签: excel xls
3条回答
倾城 Initia
3楼-- · 2019-09-13 19:02

I highly recommend Spreadsheet::WriteExcel for your needs. The library is entirely written in Perl, so all you need to do is upload the CPAN library to your web site and specify the specific location. The library documentation and the code snippet below should get you started.

#!/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();
查看更多
干净又极端
4楼-- · 2019-09-13 19:02

If you don't need super-fancy functionality such as rich text, you can use Spreadsheet::WriteExcel which works quite nicely and is pretty low-overhead too.

Edit: use my $workbook = Spreadsheet::WriteExcel->new('-'); to have your workbook written directly to STDOUT.

查看更多
登录 后发表回答