Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: Display Report Using Excel

by chime (Friar)
on Jul 07, 2004 at 09:17 UTC ( [id://372326]=note: print w/replies, xml ) Need Help??


in reply to Display Report Using Excel

If you have your data in a csv file the next step is just to put in this sub routine to create an excel file.

If your data is not in a csv but in another type of file format - you can change the 'comma' split part to something else if your data is seperated by just whitespace or another seperater.

The code is just to give you a pointer in the right direction.
You should read the Spreadsheet::WriteExcel module information as well.

The code is below

use Spreadsheet::WriteExcel; # Create an excel file binary &create_excel_file ($csv_file_name, $xls_file_name); ## This sub routine creates an xls binary from the csv file data sub create_excel_file { # create Excel binary format outfile from CSV my ($csv_file, $xls_file) = @_; my $workbook = ""; my $worksheet = ""; my $row = 0; my $column = 0; # Sending the csv file information to xls format open (CSV, $csv_file) or die "Error opening $csv_file: $!"; $workbook = Spreadsheet::WriteExcel -> new($xls_file); # Add a worksheet to the workbook file $worksheet = $workbook -> addworksheet(); # Or add a named worksheet to create @worksheets #$worksheet = $workbook->addworksheet($sheetname); #activate($sheetname) # While there is data in the csv file while (<CSV>) { chomp; # Split the fields on only the , and space seperators my @FIELDS = split(', ', $_); $column = 0; foreach my $token (@FIELDS) { # Write out the row and column and token to each row $worksheet -> write($row, $column, $token); $column++; } $row++; } # my $format1 = $workbook -> addformat(); # $format1 -> set_bold(); # $format1 -> set_align('center'); # $format1 -> set_border(); # $worksheet -> set_row(0, undef, $format1); $workbook -> close() or die "Error closing $workbook: $!"; # WARNING .... This code will delete the CSV # Delete the csv file to save space unlink ($csv_file); }

Replies are listed 'Best First'.
Re^2: Display Report Using Excel
by jdporter (Paladin) on Jul 29, 2004 at 16:16 UTC

    Um, -- you for re-inventing the CSV parser. The most obvious problem is that CSV fields can be enclosed in quotes, and when so, can contain commas, which should not be considered delimiters. Text::CSV does the right thing.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2024-04-18 05:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found