Reputation: 11
I need to convert my excel file (.xls) to tab delimited text file(.txt) in unix. Can anyone help me on this.
For eg. if i upload File.xls in server it should be converted as File.txt
Thanks for your replies.
Upvotes: 0
Views: 1455
Reputation: 207465
This should do what you want:
#!/usr/bin/perl -w
use strict;
use Spreadsheet::ParseExcel;
my $parser = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse($ARGV[0]);
if ( !defined $workbook ) {
die $parser->error(), ".\n";
}
for my $worksheet ( $workbook->worksheets() ) {
my ( $row_min, $row_max ) = $worksheet->row_range();
my ( $col_min, $col_max ) = $worksheet->col_range();
for my $row ( $row_min .. $row_max ) {
my $line="";
my $comma="";
for my $col ( $col_min .. $col_max ) {
my $cell = $worksheet->get_cell( $row, $col );
next unless $cell;
$line .= $comma;
$line .= $cell->unformatted();
# Could be $cell->unformatted() or $cell->value()
$comma=",";
}
print $line,"\n";
}
}
Save the file as xls2csv
then go to a terminal and make it executable with:
chmod +x xls2csv
Then you can run it with:
./xls2csv file.xls > file.csv
If you don't know how to install the spreadsheet module, you can do this:
sudo perl -MCPAN -e shell
cpan> install Spreadsheet::ParseExcel
cpan> q
Upvotes: 1