use strict; use Win32::OLE qw(in with); use Win32::OLE::Const 'Microsoft Excel';
$Win32::OLE::Warn = 3; # die on errors...
# get already active Excel application or open new my $Excel = Win32::OLE->GetActiveObject('Excel.Application') || Win32::OLE->new('Excel.Application', 'Quit');
# open Excel file my $Book = $Excel->Workbooks->Open("c:/komodo projects/test.xls");
# You can dynamically obtain the number of worksheets, rows, and columns # through the Excel OLE interface. Excel's Visual Basic Editor has more # information on the Excel OLE interface. Here we just use the first # worksheet, rows 1 through 4 and columns 1 through 3.
# select worksheet number 1 (you can also select a worksheet by name) my $Sheet = $Book->Worksheets(1);
foreach my $row (1..4) { foreach my $col (1..3) { # skip empty cells next unless defined $Sheet->Cells($row,$col)->{'Value'};
# print out the contents of a cell printf "At ($row, $col) the value is %s and the formula is %s\n", $Sheet->Cells($row,$col)->{'Value'}, $Sheet->Cells($row,$col)->{'Formula'}; } }
# clean up after ourselves $Book->Close;
请注意,您可以用以下方式很轻松地为单元分配值:
$sheet->Cells($row, $col)->{'Value'} = 1;
Linux 示例:解析 本节适用于 UNIX,特别适用于 Linux。没有在 Windows 中测试它。
很难给出一个比 Spreadsheet::ParseExcel 模块文档中所提供的示例更好的 Linux 解析示例,因此我将演示那个示例,然后解释其工作原理。
下载 parse-excel.pl
清单 3:parse-excel.pl
#!/usr/bin/perl -w
use strict; use Spreadsheet::ParseExcel;
my $oExcel = new Spreadsheet::ParseExcel;
die "You must provide a filename to $0 to be parsed as an Excel file" unless @ARGV;
# sanity checking on the source file: rows and columns should be sensible next unless defined $source_sheet->{MaxRow}; next unless $source_sheet->{MinRow} <= $source_sheet->{MaxRow}; next unless defined $source_sheet->{MaxCol}; next unless $source_sheet->{MinCol} <= $source_sheet->{MaxCol};
foreach my $row_index ($source_sheet->{MinRow} .. $source_sheet->{MaxRow}) { foreach my $col_index ($source_sheet->{MinCol} .. $source_sheet->{MaxCol}) { my $source_cell = $source_sheet->{Cells}[$row_index][$col_index]; if ($source_cell) { print "( $row_index , $col_index ) =>", $source_cell->Value, "\n";
if ($source_cell->{Type} eq 'Numeric') { $storage_book->{$source_sheet->{Name}}->{$row_index}->{$col_index} = $source_cell->Value*2; } else { $storage_book->{$source_sheet->{Name}}->{$row_index}->{$col_index} = $source_cell->Value; } # end of if/else } # end of source_cell check } # foreach col_index } # foreach row_index } # foreach source_sheet_number
print "Perl recognized the following data (sheet/row/column order):\n"; print Dumper $storage_book;
my $dest_book = Spreadsheet::WriteExcel->new("$destname") or die "Could not create a new Excel file in $destname: $!";
print "\n\nSaving recognized data in $destname...";
foreach my $sheet (keys %$storage_book) { my $dest_sheet = $dest_book->addworksheet($sheet); foreach my $row (keys %{$storage_book->{$sheet}}) { foreach my $col (keys %{$storage_book->{$sheet}->{$row}}) { $dest_sheet->write($row, $col, $storage_book->{$sheet}->{$row}->{$col}); } # foreach column } # foreach row } # foreach sheet