tags:

views:

448

answers:

3

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 colors. Any one can help?

+1  A: 

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.

Dan
Thanks Dan for your reply, but is there a way to do that without having to install any libraries, since I need to put my code on a server that I don't have much permissions at. Thanks again.
zeina
+1  A: 

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