【发布时间】:2011-03-02 05:14:04
【问题描述】:
我有一个巨大的 xlsx 文件(大约 127 MB)并想使用 Spreadsheet::Excel 模块读取,但在 2GB RAM 机器上出现“内存不足”错误。 (注意该脚本适用于较小的 excel 2007 文件)
有没有什么方法可以逐行读取excel文件而不会达到内存限制?搜索谷歌我遇到了http://discuss.joelonsoftware.com/default.asp?joel.3.160328.14,但我不熟悉如何将电子表格存储到标量中。有人可以给我一个将 excel 2007 文件读取为标量和打印单元格值的示例。 下面是我在较小的电子表格上运行的当前脚本。
#!/usr/bin/perl
use Excel::Writer::XLSX;
use Spreadsheet::XLSX;
my $workbook = Excel::Writer::XLSX->new('Book1.xlsx');
my $worksheet = $workbook->add_worksheet();
# use strict;
my $excel = Spreadsheet::XLSX -> new ('Book2.xlsx');
my $date_format = $workbook->add_format();
$date_format->set_num_format('dd/mm/yy hh:mm');
# Columns of interest
@columns=(0,1,2,5,9,10,12,13,31);
@reportlist=("string1","String2","String3");
@actuallist=("ModifiedString1","ModifiedString2","ModifiedString3");
$max_list=$#reportlist;
foreach my $sheet (@{$excel -> {Worksheet}}) {
printf("Sheet: %s\n", $sheet->{Name});
$sheet -> {MaxRow} ||= $sheet -> {MinRow};
foreach my $row ($sheet -> {MinRow} .. $sheet -> {MaxRow}) {
$sheet -> {MaxCol} ||= $sheet -> {MinCol};
for ($c=0;$c<=$#columns;$c++){
$col=$columns[$c];
my $cell = $sheet -> {Cells} [$row] [$col];
if($col==0){
$cell->{Val}=~ s/\ GMT\+11\:00//g;
$worksheet->write($row,$c,$cell->{Val},$date_format);
}
if ($cell) {
$worksheet->write($row,$c,$cell -> {Val});
for($z=0;$z<=$#reportisplist;$z++){
if(($cell->{Val})=~ m/$reportlist[$z]/i){
$worksheet->write($row,$c,$actuallist[$z]);
}
}
}
}
}
}
$workbook->close();
【问题讨论】:
标签: perl excel spreadsheet