1833847 Members
2193 Online
110063 Solutions
New Discussion

Perl problem

 
SOLVED
Go to solution
Jeff Picton
Regular Advisor

Perl problem

Hi

I am using the perl module Spreadsheet::WriteExcel to write data to an xls file.

I know how to write individual data to a new file, but can anyone tell me how to write the contents of a data file (eg a text file) to a new xls file using the module ?
3 REPLIES 3
Steven E. Protter
Exalted Contributor

Re: Perl problem

Microsoft is a little secretive about how to create excel files without licensing excel.

The easiet way to go is to write it out ascii text, perhaps comma delimited, so excel can properly import it.

This will not help much with forumlas though.

SEP
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com
Stanimir
Trusted Contributor
Solution

Re: Perl problem

Hi!
I thing, this will help:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::WriteExcel;

# Check for valid number of arguments
if (($#ARGV < 1) || ($#ARGV > 2)) {
die("Usage: tab2xls tabfile.txt newfile.xls\n");
};

# Open the tab-delimited file
open (TABFILE, $ARGV[0]) or die "$ARGV[0]: $!";

# Create a new Excel workbook
my $workbook = Spreadsheet::WriteExcel->new($ARGV[1]);
my $worksheet = $workbook->addworksheet();
# Row and column are zero indexed
my $row = 0;

while () {
chomp;
# Split on single tab
my @Fld = split('\t', $_);

my $col = 0;
foreach my $token (@Fld) {
$worksheet->write($row, $col, $token);
$col++;
}
$row++;

Regards,Stan
Jeff Picton
Regular Advisor

Re: Perl problem

Hi

Thanks for that - I now know that the package has useful examples.