Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw

Re: *log to *.xls

by 2teez (Priest)
on Dec 19, 2012 at 10:00 UTC ( #1009538=note: print w/replies, xml ) Need Help??

in reply to *log to *.xls

"..I want to convert the log files to xls. by bash script I converted it to csv.."

Since you have a csv file already, you will need Text::CSV_XS or Text::CSV module to read in your csv file and Spreadsheet::WriteExcel to write into an excel file as mentioned by marto.
Something like so:

#!/usr/bin/perl use strict; use warnings; use utf8; use Spreadsheet::WriteExcel; use Text::CSV_XS; my $csv = Text::CSV_XS->new( { binary => 1, allow_loose_quotes => 1, } + ) or die "can't open CSV file" . Text::CSV_XS->error_diag(); my $wrkbook = Spreadsheet::WriteExcel->new('new_file.xls'); my $wrksheet = $wrkbook->add_worksheet('new_file'); open my $fh, "<:encoding(utf8)", "Data.csv" or die "can't open file: $ +!"; while ( my $row = $csv->getline($fh) ) { $wrksheet->write_row( $., 0, [ @{$row} ] ); ## write to excel file } $csv->eof or $csv->error->diag(); close $fh or die "can't close file:$!";

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

Replies are listed 'Best First'.
Re^2: *log to *.xls
by Tux (Abbot) on Dec 19, 2012 at 19:52 UTC

    Or use csv2xls out of Text::CSV_XS' examples folder:

    $ csv2xls --help usage: csv2xls [-s <sep>] [-q <quot>] [-w <width>] [-d <dtfmt>] [-o <xls>] [file.csv] -s <sep> use <sep> as seperator char. Auto-detect, default += ',' The string "tab" is allowed. -e <esc> use <sep> as seperator char. Auto-detect, default += ',' The string "undef" is allowed. -q <quot> use <quot> as quotation char. Default = '"' The string "undef" will disable quotation. -w <width> use <width> as default minimum column width (4) -o <xls> write output to file named <xls>, defaults to input file name with .csv replaced with .xls if from standard input, defaults to csv2xls.xls -F allow formula's. Otherwise fields starting with an equal sign are forced to string -f force usage of <xls> if already exists (unlink befor +e use) -d <dtfmt> use <dtfmt> as date formats. Default = 'dd-mm-yyyy +' -D cols only convert dates in columns <cols>. Default is eve +rywhere. -u CSV is UTF8 -v [<lvl>] verbosity (default = 1)

    Enjoy, Have FUN! H.Merijn
Re^2: *log to *.xls
by frhling (Initiate) on Dec 20, 2012 at 12:34 UTC
    Thanks. I have tried to run this, but I have an Error: "" Spreadsheet/ did not return a true value at line 6. BEGIN failed--compilation aborted at line 6."" in folder C:/Test where I run the scripts, I have: ActivePerl- Spreadsheet-ParseExcel-0.2602 Spreadsheet-WriteExcel-2.38 I dont know what else I should do so that I get rid out of this error. I would appreciated if you help me out.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1009538]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-05-29 18:14 GMT
Find Nodes?
    Voting Booth?