2014-02-14 247 views
1

转换的.xls我的Perl脚本为.csv将.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; 
} 

糟糕! at csv.pl line 23.

投掷错误。我的代码有什么问题?请在我的代码中找到错误。 在此先感谢。

+0

$ CSV最初指向的路径。那你为什么要将数据附加到变量上。当你打开文件(我的$ FH,'>',“$ csv”)或者死掉“oops!”时;它是否包含名称或内容? – Raghuram

+0

它只包含内容 – Ram

+1

如果这个问题与[你以前的问题](http://stackoverflow.com/q/21771708/725418)(它似乎是)是相同的,你应该添加新的信息,而不是打开一个新的问题。其中一个问题可能因为重复而搁置。 – TLP

回答

0

你有一个错字:

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

使用严格和警告,你在编译时得到警告。

use strict; 
use warnings; 

你有错别字:

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

while (my$line = <$FH>){ 

问候,

+0

我用'严格使用; 使用警告;'但仍然是相同的错误 – Ram