Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Exporting Data to and Excel file format

by curtisb (Monk)
on Jun 09, 2005 at 19:59 UTC ( [id://465287]=perlquestion: print w/replies, xml ) Need Help??

curtisb has asked for the wisdom of the Perl Monks concerning the following question:

All,

I have a flat file that I'm parsing with perl.

I would like to output the parsed data to an excel file format file. This data is comma delimited and I want each column of data to appear in its own column.

If anyone has any ideas please let me know.

Thanks

Bobby Curtis

b.curtis@stanleyassociates.com

  • Comment on Exporting Data to and Excel file format

Replies are listed 'Best First'.
Re: Exporting Data to and Excel file format
by kirbyk (Friar) on Jun 09, 2005 at 20:03 UTC
    While there are Excel modules, do you really need to do this step? Excel loads up CSVs no problem. I do this on a daily basis. There are certain Excel features that you can't do with a CSV, but if you just want, say, business people to be able to double click on a file and see it in excel, just give them the csv and it will work.

    -- Kirby, WhitePages.com

Re: Exporting Data to and Excel file format
by mifflin (Curate) on Jun 09, 2005 at 20:27 UTC
    use Spreadsheet::WriteExcel; $workbook = Spreadsheet::WriteExcel->new('sample.xls'); $worksheet = $workbook->add_worksheet('ws name'); $worksheet->write(0,0,'something in a1'); $workbook->close();
    Update: here's something more specific. It will read a csv file and write it to an Excel file.
    use IO::File; use Spreadsheet::WriteExcel; my $infile = IO::File->new("<infile.csv"); my $workbook = Spreadsheet::WriteExcel->new('output.xls'); my$worksheet = $workbook->add_worksheet('ws name'); my $row = 0; while (my $line = $infile->getline()) { chomp($line); my $record = [split ',', $line]; $worksheet->write_row($row,0, $record); $row++; } $infile->close(); $workbook->close();
Re: Exporting Data to and Excel file format
by davidrw (Prior) on Jun 09, 2005 at 20:58 UTC
    This node will be of interest: Index of Spreadsheet FAQs

    What is the context here? As stated above, you can just fake it with text. To do this in cgi, just need to tab-delimit and set the proper content-type header:
    print "Content-type: application/vnd.ms-excel\n\n"; print join("\t", @cols) . "\n"; foreach my $row ( @rows ){ print join("\t", @$row) . "\n"; # if rows are array refs print join("\t", @{$row}{@cols} ) . "\n"; # if rows are hash refs }
Re: Exporting Data to and Excel file format
by Xaositect (Friar) on Jun 09, 2005 at 21:26 UTC

    Outputting a file.csv and opening it in Excel works fine, but doesn't have that classic ".xls" extension.

    Outputting a CSV file as file.xls doesn't work, (at least, with my version of office) but saving tab-delinated data as file.xls works fine.

    However, both the above solutions above will cause a prompt if the user tries to save the file: "file.xls may contain features that are not compatiable..." This may be confusing if the user doesn't understand why they are getting the message.


    I've worked with Spreadsheet::ParseExcel quite a bit, and if you need to output native excel, it's a pretty good module. You can add formatting like freezing the header row so it scrolls with the data, and fancy stuff like font formatting.

    If you want to get really tricky, and you're comfortable with XML, create a spreadsheet with sample data and all the formatting and features you want, and save it as XML. Then write perl to substitute the sample data with the data you want to display. If you're careful to output the same tags that Excel used originally, you get a native-excel file with all the advanced features you could want.

    Not suprisingly, Excel is pretty picky that you give it the XML format it expects, but with an existing file as a template, it's not that difficult.

Re: Exporting Data to and Excel file format
by djohnston (Monk) on Jun 09, 2005 at 21:02 UTC
    As was already stated, Excel will open a csv file no problem. If you want, you can even rename it using a .xls suffix to fool everyone into thinking it's a native Excel document. That's what I do, but a) I don't know any better and b) I don't have to worry about getting fired for being a liar.

    Update:
    My data is not comma delimited, it's tab delimited. I can't say if this works with comma delimited files (without lying, that is). Disregard my worthless advice.

      D'oh! I posted from the preview-able window. Argh! What do you do when this happens?

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://465287]
Approved by moot
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-20 10:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found