我是perl的新手,我刚刚开始使用模块Spreadsheet :: ParseExcel。 我不想在我正在使用的Excel工作表上写任何东西。 该脚本应基本上通过Excel工作表运行并逐行打印值。 我有大约250行和3列。 所以输出必须是这样的:
Glendale Academy Mark 40%
Glendale Academy Tom 60%
.....
.....
.....
在终端上。
我正在使用这个Windows(我应该考虑Win32::OLE
和Win32::OLE::Const 'Microsoft Excel'
。(表格的格式应该是什么?.xls,.xlsx,.csv)
这是我到目前为止所做的:
经过这个网站上的大量脚本之后,我认为使用“Split”将是最简单的。现在它非常简单,因为它很棒我无法理解如何使用它。
到目前为止我的脚本是这样的;
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";
}
}
}
任何形式的帮助都会非常感谢大家; 现在坚持了一个星期....
答案 0 :(得分:1)
初始计划有一些问题:
Excel::Write::XLSX
,未导入且与Spreadsheet :: ParseExcel或读取数据无关。Spreadsheet::WriteExcel
,它不会导入,也不会与Spreadsheet :: ParseExcel或读取数据有任何关系。next_row
和next_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