简体   繁体   English

使用Spreadsheet :: ParseExcel时出错

[英]Error when using Spreadsheet::ParseExcel

I get 2 errors when compiling the following code: 编译以下代码时出现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 .= ",";
        }
    }
}

Errors: 错误:

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

I get the above code from http://www.ehow.com/how_7352636_convert-xls-csv-perl.html , and installed the excel module using: cpan Spreadsheet::ParseExcel Spreadsheet::XLSX Spreadsheet::Read 我从http://www.ehow.com/how_7352636_convert-xls-csv-perl.html获得了上述代码,并使用以下代码安装了excel模块: cpan Spreadsheet::ParseExcel Spreadsheet::XLSX Spreadsheet::Read

What's causing the error? 是什么导致错误?

Those errors mean that you are using strict , but you didn't declare some variables with my . 这些错误表示您使用的是strict ,但是您并未在my脚本中声明一些变量。 For example, you declared $xmlprser , but then you tried to use $parser , which was not declared. 例如,您声明了$xmlprser ,但是随后尝试使用未声明的$parser The code you copied has errors. 您复制的代码有错误。

A better place to get code is the source itself: Spreadsheet::ParseExcel 获得代码的更好的地方是源代码本身: Spreadsheet :: ParseExcel

Try: 尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM