2014-02-14 70 views
3

如何将.xls转换为.csv in perl?这是什么模块?有没有这样的例子? 什么是转换的最佳方式?如何把.xls文件转换为.csv文件?

use Spreadsheet::ParseExcel; 
my $xlsparser = Spreadsheet::ParseExcel->new(); 
my $xlsbook = $xlsparser->parse('/home/Admin/Downloads/abc.xls'); 
my $xls = $xlsbook->worksheet(0); 
my ($row_first, $row_last) = $xls->row_range(); 
my ($col_first, $col_last) = $xls->col_range(); 
my $csv = '/home/Admin/Downloads/ram.csv'; 
for my $row ($row_first .. $row_last) {  # Step through each row 
    for my $col ($col_first .. $col_last) { # Step through each column 
     my $cell = $xls->get_cell($row, $col); # Get the current cell 
     next unless $cell; 
     $csv .= $cell->unformatted(); # Get the cell's raw data -- no border 
             # colors or anything like that 
     if ($col == $col_last) { 
      $csv .= "\n"; 
     } else { 
      $csv .= ","; 
     } 
    } 
} 
open(my$FH ,'>',"$csv") or die "oops!"; 

while (my$line = <$xlsbook>){ 
    print $FH $line; 
} 

哎呀!在csv.pl第23行。

+0

一些很好的参考http://www.ehow.com/how_7352636_convert-xls-csv-perl.html http://vinayhacks.blogspot.in/2010/04/converting-xls-to-csv- on-linux.html –

+0

请参阅我的更新问题 – Ram

+0

[将.xls文件转换为.csv文件?](http://stackoverflow.com/questions/21772377/converting-xls-file-to-csv-file) –

回答

1

使用perl脚本。使用CPAN中的Spreadsheet :: ParseExcel perl模块解析xls文件,然后输出为csv应该可以正常工作。

Here is the link

and this link

+0

请参阅我的更新问题 – Ram

1

你在你的代码输入错误。

open(my $FH ,'>',"$csv") or die "oops!"; 

while (my $line = <$FH>){ 
    print $FH $line; 
} 
+0

我在csv.pl第26行得到了不是GLOB引用。此错误 – Ram

+0

修复了变量名称,请再试一次。 – user1126070

+0

我认为这是错误的打开(我的$ FH,'>',“$ csv”)或死“哎呀!”; (我的$行= <$FH>){ ' – Ram