我需要在unix中将我的excel文件(.xls)转换为制表符分隔的文本文件(.txt)。有人能帮我这个忙吗。
例如。如果我在服务器上载File.xls,它应该被转换为File.txt
感谢您的回复。
发布于 2014-07-18 20:04:51
这应该是您想要的结果:
#!/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";
}
}将文件另存为xls2csv,然后转到终端并使用以下命令使其可执行:
chmod +x xls2csv然后,您可以使用以下命令运行它:
./xls2csv file.xls > file.csv如果您不知道如何安装电子表格模块,您可以这样做:
sudo perl -MCPAN -e shell
cpan> install Spreadsheet::ParseExcel
cpan> qhttps://stackoverflow.com/questions/24800406
复制相似问题