#!/usr/bin/perl 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'); my @files = ("e:\\perl\\test.xlsx"); my $value = 0; my %hash = { "北京" => (), "天津" => (), }; foreach my $file (@files){ my $book = $Excel->Workbooks->Open( $file ); foreach my $Sheet (in $book->{Worksheets}) { my $sheetName = $Sheet->{Name}; print "$sheetName\n"; my $minRow = 1; my $maxRow = $Sheet->UsedRange->Rows->Count; my $minCol = 2; my $maxCol = $Sheet->UsedRange->Columns->Count; foreach my $row ( $minRow .. $maxRow ){ my $cell_value = $Sheet->Cells($row,1)->{Value}; next unless defined $cell_value; if(exists $hash{$cell_value}){ foreach my $col ( $minCol .. $maxCol ){ my $value = $Sheet->Cells($row,$col)->{Value}; next unless defined $value; push $hash{$cell_value}, $value; } } } } $book->Close(); } foreach my $key (keys %hash){ print "$key $hash{$key}\n"; } $Excel->Quit();
perl处理excelwenjian
最新推荐文章于 2024-06-16 17:46:30 发布