For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > January 2006 > Convert Excel to Text (Specific Tab)









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Convert Excel to Text (Specific Tab)
lealvf@gmail.com

2006-01-10, 4:02 am

I have the following code that takes an excel spreadsheet and converts
it to a PIPE delimited text file. However, this will create a text
file for EVERY tab and what I would like to do is tell the script which
specific tab in a spreadsheet to use. For example, if I have a tab
called "employee" I would like to pass in a parameter value of
"employee" to my Perl script and it converts that excel file's tab to a
pipe delimited format.

#### BEGIN CODE ####
use Spreadsheet::ParseExcel::Simple;

# These are my parameters that I am passing in
my ($p_src_file) = @ARGV[0];
my ($p_sheet) = @ARGV[1];
my ($p_target_file) = @ARGV[2];

my $xls = Spreadsheet::ParseExcel::Simple->read($p_src_file) or die
"Can't read $p_src_file: $!\a";

my $index = 1;
foreach my $sheet ($xls->sheets)
{
open(TXT, ">$p_target_file$index.TXT");
while ($sheet->has_data)
{
my @row = $sheet->next_row;
foreach (@row)
{
if (/[,"]/)
{
s/"/""/g;
s/^/"/;
s/$/"/;
}
}
print TXT join('|', @row), "\n";
}
close TXT;
} continue
{
$index++;
}

#### END CODE ####

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com