使用Spreadsheet :: ParseExcel时出错

编译以下代码时出现2个错误:

#!/usr/bin/perl use strict; use warnings; use Spreadsheet::ParseExcel; my $xlsparser = Spreadsheet::ParseExcel->new(); my $xlsbook = $parser->parse('xsl_test.xls'); my $xls = $xls->worksheet(0); my ( $row_first, $row_last ) = $xls->row_range(); my ( $col_first, $col_last ) = $xls->col_range(); my $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"; #Make a new line at the end of the row } else { $csv .= ","; } } } 

错误:

 global symbol "$parser" requires explicit package name at line 8 global symbol "$xls" requires explicit package name at line 9 

我从http://www.ehow.com/how_7352636_convert-xls-csv-perl.html获得上述代码,并使用以下命令安装了excel模块: cpan Spreadsheet::ParseExcel Spreadsheet::XLSX Spreadsheet::Read

什么导致了错误?

这些错误意味着你正在使用strict ,但你没有用my声明一些variables。 例如,你声明了$xmlprser ,但是然后你试图使用$parser ,这是没有声明的。 您复制的代码有错误。

获取代码的更好的地方是源自身: Spreadsheet :: ParseExcel

尝试:

 my $parser = Spreadsheet::ParseExcel->new(); my $xlsbook = $parser->parse('xsl_test.xls'); my $xls = $xlsbook->worksheet(0);