LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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 01-15-2010, 01:35 PM   #1
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Rep: Reputation: 15
Can't write header to excel file


I am having trouble getting some code to work. It takes an input file (csv) and converts it to an XLS. The problem is that I can't seem to get a header to be printed first...

Code:
use Spreadsheet::WriteExcel;

my $input = $ARGV[0];
my $output = $input;
$output =~ s/\.csv$/.xls/i;

my $wb = Spreadsheet::WriteExcel->new("$output");
my $sheet = $wb->addworksheet("My Sheet");

open(CSV, "$input");

while (<CSV>) {
    print CSV ">header, header, header";
    chomp;
    my(@vals)=split(/,/, $_);
    my $format = ($. == 1) ? $bluebg : undef;
    foreach my $vidx (0..$#vals) {
        $sheet->write(($.-1), $vidx, $vals[$vidx], $format);
    }

}
 
Old 01-16-2010, 12:46 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by hawk__0 View Post
I am having trouble getting some code to work. It takes an input file (csv) and converts it to an XLS. The problem is that I can't seem to get a header to be printed first...

Code:
use Spreadsheet::WriteExcel;

my $input = $ARGV[0];
my $output = $input;
$output =~ s/\.csv$/.xls/i;

my $wb = Spreadsheet::WriteExcel->new("$output");
my $sheet = $wb->addworksheet("My Sheet");

open(CSV, "$input");

while (<CSV>) {
    print CSV ">header, header, header";
    chomp;
    my(@vals)=split(/,/, $_);
    my $format = ($. == 1) ? $bluebg : undef;
    foreach my $vidx (0..$#vals) {
        $sheet->write(($.-1), $vidx, $vals[$vidx], $format);
    }

}
Ok...how about telling us what the actual error(s) are, version/distro of Linux, etc. Some details would help....
 
Old 01-16-2010, 01:00 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
What exactly is the point of?
Code:
 print CSV ">header, header, header";
    chomp;
 
Old 01-18-2010, 10:37 AM   #4
hawk__0
Member
 
Registered: Nov 2008
Posts: 105

Original Poster
Rep: Reputation: 15
There is no error, it's just not working. I'm not that good at perl and the point of that code was to attempt to output a header to the file.
 
Old 01-18-2010, 03:52 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,617

Rep: Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963Reputation: 7963
Quote:
Originally Posted by hawk__0 View Post
There is no error, it's just not working. I'm not that good at perl and the point of that code was to attempt to output a header to the file.
Well, you're trying to output a bogus XLS 'header', but you're trying to write it to the file you're opening for INPUT.

Read the description pages for the spreadsheet::writeexcel module here:
http://search.cpan.org/~jmcnamara/Sp.../WriteExcel.pm

which has examples on how to do this. One of the examples is for doing a tab-separated file, which could easily be modified to do comma-separated instead.

You might also want to check out some Perl tutorials, which cover opening files. http://www.perlmonks.com is a good place to start.
 
Old 01-18-2010, 10:40 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,356

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials
 
  


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
Excel 2007 file not opening rietsit Linux - Software 2 09-08-2009 09:33 AM
excel file magically deleted? LarryFrigginWachs Linux - Newbie 6 12-13-2005 02:01 PM
convert text file to binary excel file ust Linux - General 2 11-23-2004 02:33 AM
How to write Makefile in 2.6 for including header files in another dir ? ashbalu Programming 0 10-19-2004 03:45 PM
How do I custom write a library and use it in the header declaration ? Linh Programming 4 05-26-2004 03:38 PM

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

All times are GMT -5. The time now is 09:53 PM.

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