excel2txt.pl
#!/usr/bin/perl
use strict;
use Spreadsheet::ParseExcel;
my $oExcel = new Spreadsheet::ParseExcel;
die "Usage : \n\tperl $0 <xls> > xls.txt\n\n" unless @ARGV;
my $oBook = $oExcel->Parse($ARGV[0]);
my($iR, $iC, $oWkS, $oWkC);
for(my $iSheet=0; $iSheet < $oBook->{SheetCount} ; $iSheet++){
$oWkS = $oBook->{Worksheet}[$iSheet];
for(my $iR = $oWkS->{MinRow} ; defined $oWkS->{MaxRow} && $iR <= $oWkS->{MaxRow} ; $iR++) {
for(my $iC = $oWkS->{MinCol} ; defined $oWkS->{MaxCol} && $iC <= $oWkS->{MaxCol} ; $iC++) {
$oWkC = $oWkS->{Cells}[$iR][$iC];
print $oWkC->Value,"\t" if($oWkC);
}
print "\n";
}
}
输入为xls格式,集群下可用。
txt2excel.pl
#!/usr/bin/perl
use strict;
use Encode qw(decode);
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("$ARGV[0].xls");
my $worksheet = $workbook->add_worksheet();
# Row and column are zero indexed
my $row = 0;
open IN,$ARGV[0] or die $!;
while(<IN>){
chomp;my @s = split;
my $col = 0;
foreach my $token(@s){
$worksheet->write_string($row,$col,decode('gb2312',$token));
$col++;
}
$row++;
}
close IN;