How to save data on excel xls file using perl?

跟風遠走 提交于 2019-12-25 03:34:08

问题


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?


回答1:


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();



回答2:


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.




回答3:


http://www.ibm.com/developerworks/library/l-pexcel/

http://search.cpan.org/dist/DBD-Excel/



来源:https://stackoverflow.com/questions/1833820/how-to-save-data-on-excel-xls-file-using-perl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!