如何parsingzip文件中的excel文件?

我希望能够parsing一个zip文件中的excel。 我已经能够parsing压缩文件,以返回该压缩文件中的文件,如果正则expression式匹配带来了一个Excel文件,我想parsing文件。

以下是parsingzip文件的excel电子表格名称的脚本。

#!/usr/bin/perl use strict; use warnings; use Archive::Zip; use Spreadsheet::ParseExcel; my $zipFile = Archive::Zip->new(); my $xl_file = ""; #open zipfile $zipFile->read( '/home/user/Desktop/test.zip' ) == 0 || die "cannot read zip file\n"; #find all files within zipfile my @files = $zipFile->memberNames('/home/user/Desktop/test.zip'); foreach my $file (sort @files) { #find all excel files if($file =~ m/(.*xls)/){ $xl_file = $1; print "excel file found.\n"; } } 

这是parsing单元格中的值的脚本。

 #!/usr/bin/perl use strict; use warnings; my $filename = "/home/user/worksheet.xls"; use Spreadsheet::ParseExcel; my $parser = Spreadsheet::ParseExcel->new(); my $workbook = $parser->parse("$filename"); if ( !defined $workbook ) { die $parser->error(), ".\n"; } open(FILE, '>', "parse.txt")||die "cannot open parse.txt!\n"; for my $worksheet ( $workbook->worksheets() ) { my ( $row_min, $row_max ) = $worksheet->row_range(); my ( $col_min, $col_max ) = $worksheet->col_range(); my $s = $worksheet -> get_cell(2,2); my $p = $worksheet-> get_cell(2,3); print FILE $s->value()."\n"; print FILE $p->value()."\n"; } close FILE; 

我如何将这些整合在一起?

根据Archive::Zip的文档,可以将压缩文件成员的内容作为string来获取:

 $xls_content = $zipFile->contents($file); 

根据Spreadsheet::ParseExcel的文档,可以通过传递string作为参考来parsing包含Excel文件内容的string:

 my $workbook = $parser->parse(\$xls_content); 

所以你应该能够把两者结合在一起。

另一种可能是将压缩文件成员解压缩到临时文件中。