LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-15-2009, 01:52 PM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
Perl convert csv to xls


I am not good at perl, in fact, I hadrly understand it. I found this script on the internet that will convert csv to xls:

Code:
#!/usr/bin/perl
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
open(FH,"<file1.csv") or die "Cannot open file: $!\n";
my ($x,$y) = (0,0);
while (<FH>){
 chomp;
 @list = split /\s+/,$_;
 foreach my $c (@list){
    $worksheet->write($x, $y++, $c);
 }
 $x++;$y=0;
}
close(FH);
$workbook->close();
It works great, aside from the problem with how it chooses to make it space delimited. Is there a way to make it comma delimited?

Also, is it possible to change it so that "file1.csv" is a command line argument?

Thanks!
 
Old 09-15-2009, 05:27 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
Quote:
Originally Posted by hawk__0 View Post
Code:
@list = split /\s+/,$_;
it is this call to the perl function "split" that makes it "whitespace delimited". this line is saying "take the variable named $_, split it into an array where each one or more 'whitespaces' means a new element in this array, and store the resulting array into the variable named @list". the delimiter is "\s+" which says "one or more (white)spaces". to make it seem comma delimited change "\s+" to ","

edit:
you will see weird behaviour when values in the input CSV file contain your delimiter, ",". if the input file looks like:
Code:
header1,header2,header3
value1,"value2,",value3
then the "split" function will return the the following array (i will put each item on its own line):
Code:
header1
header2
header3
value1
"value2
"
value3
because of this you will have to make it "smarter" and do some checks/string manipulation on the input. one example may be to change the regular expression that your passing to "split". note: the current script will have problems if the values contain the delimiter it is using, which is whitespace, so this problem isnt because your changing it to comma delimited, but because the script initially doesnt handle these cases (which may be very likely to occur).

Last edited by nadroj; 09-15-2009 at 05:36 PM.
 
Old 09-15-2009, 07:48 PM   #3
Sergei Steshenko
Senior Member
 
Registered: May 2005
Posts: 4,481

Rep: Reputation: 454Reputation: 454Reputation: 454Reputation: 454Reputation: 454
Quote:
Originally Posted by hawk__0 View Post
I am not good at perl, in fact, I hadrly understand it. I found this script on the internet that will convert csv to xls:

Code:
#!/usr/bin/perl
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("test.xls");
my $worksheet = $workbook->add_worksheet();
open(FH,"<file1.csv") or die "Cannot open file: $!\n";
my ($x,$y) = (0,0);
while (<FH>){
 chomp;
 @list = split /\s+/,$_;
 foreach my $c (@list){
    $worksheet->write($x, $y++, $c);
 }
 $x++;$y=0;
}
close(FH);
$workbook->close();
It works great, aside from the problem with how it chooses to make it space delimited. Is there a way to make it comma delimited?

Also, is it possible to change it so that "file1.csv" is a command line argument?

Thanks!
From your questions it appears you've never learned Perl. Is my guess correct ?

If yes, why not to invest some effort before asking ?
 
Old 09-16-2009, 09:13 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Just as there is a (presumably) good module for creating Excel spreadsheets, there is also a number of good modules for parsing CSV files, which, as nadroj points out, requires more effort than simple string splitting. Go to cpan.org, and search the modules list for 'CSV'. Many hits will emerge. Parse::CSV and Text::CSV seem like decent first candidates.
--- rod.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
looking for a perl script to convert html table data into a csv file swiftguy121 Linux - Software 2 04-25-2007 07:28 PM
Script to convert csv 2 xls or odt xowl Linux - Software 1 01-16-2007 09:06 PM
export redhat linux csv file to xls bbgtilak Linux - General 4 12-27-2006 07:53 AM
xls to csv or ascii Shioni Linux - Software 1 08-04-2006 05:04 AM
Parsing XLS or CSV in Perl - what and what not me_the_apprentice Programming 13 02-28-2006 03:44 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration