0

我是 perl 的新手,我刚刚开始使用模块 Spreadsheet::ParseExcel。我不想在我正在使用的 Excel 工作表上写任何东西。该脚本基本上应该通过 Excel 工作表运行并逐行打印值。我有大约 250 行和 3 列。所以输出必须是这样的:

   Glendale Academy  Mark  40%
   Glendale Academy  Tom   60%
   .....
   .....
   .....

在终端上。

我正在使用这个Windows(我应该考虑Win32::OLEWin32::OLE::Const 'Microsoft Excel'。(表格的格式应该是什么?.xls,.xlsx,.csv)这是我到目前为止所做的:在浏览了这个网站上的很多脚本之后,我认为使用“拆分”将是最简单的。现在它是 excel,这非常容易,我无法理解如何使用它。

到目前为止,我的脚本是这样的;

use strict;
use warnings;
use Spreadsheet::ParseExcel;

my $reader   = Excel::Write::XLSX->new();  # I am sure something is wrong here
my $workbook  = Spreadsheet::WriteExcel->new('Draft.xls');
#my $workbook = $reader->read_file( 'Draft.xlsx' );

if ( !defined $workbook ) {
    die $reader->error(), "\n";
}

for my $worksheet ( $workbook->worksheets() ) {
    my $sheetname = $worksheet->name();
    print "Sheet = $sheetname\n";
    while ( my $row = $worksheet->next_row() ) {
        while ( my $cell = $row->next_cell() ) {
            my $row   = $cell->row();
            my $col   = $cell->col();
            my $value = $cell->value();
            print "  Cell ($row, $col) = $value\n";
        }
    }
}

任何形式的帮助将不胜感激;现在卡了一个星期……

4

1 回答 1

1

初始程序中存在一些错误:

  1. 该程序正在使用Excel::Write::XLSX其中包含拼写错误,未导入并且与 Spreadsheet::ParseExcel 或读取数据没有任何关系。
  2. 然后程序使用Spreadsheet::WriteExcel未导入且与 Spreadsheet::ParseExcel 或读取数据没有任何关系的程序。
  3. 然后该程序似乎使用 Excel::Reader::XLSX 接口(next_rownext_cell)。

如果您想从 Excel XLS 文件中读取数据,只需使用Spreadsheet::ParseExcel文档中的界面和示例程序:

#!/usr/bin/perl -w

use strict;
use Spreadsheet::ParseExcel;

my $parser   = Spreadsheet::ParseExcel->new();
my $workbook = $parser->parse('Draft.xls');

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 ) {
        for my $col ( $col_min .. $col_max ) {

            my $cell = $worksheet->get_cell( $row, $col );
            next unless $cell;

            print "Row, Col    = ($row, $col)\n";
            print "Value       = ", $cell->value(),       "\n";
            print "Unformatted = ", $cell->unformatted(), "\n";
            print "\n";
        }
    }
}

__END__

当针对您上面显示的示例电子表格运行时,会给出:

Row, Col    = (0, 0)
Value       = Glendale Academy
Unformatted = Glendale Academy

Row, Col    = (0, 1)
Value       = Mark
Unformatted = Mark

Row, Col    = (0, 2)
Value       = 40%
Unformatted = 0.4

Row, Col    = (1, 0)
Value       = Glendale Academy
Unformatted = Glendale Academy

Row, Col    = (1, 1)
Value       = Tom
Unformatted = Tom

Row, Col    = (1, 2)
Value       = 60%
Unformatted = 0.6
于 2013-03-01T11:27:11.013 回答