如何在块循环中使用PHPExcel库确定文件的结尾? [英] How do I determine the end of the file using PHPExcel library in chunk loop?
问题描述
我使用PHPExcel库尝试遍历约1500行,每行约有25列.
Using the PHPExcel library, I am attempting to iterate over around 1500 rows, each row has about 25 columns.
我正在使用此代码(摘自 PHPExcel用完了256、512和1024MB的RAM ):
I am using this code (taken from PHPExcel runs out of 256, 512 and also 1024MB of RAM):
/** Create a new Reader of the type defined in $inputFileType **/
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
/** Define how many rows we want to read for each "chunk" **/
$chunkSize = 20;
/** Create a new Instance of our Read Filter **/
$chunkFilter = new chunkReadFilter();
/** Tell the Reader that we want to use the Read Filter that we've Instantiated **/
$objReader->setReadFilter($chunkFilter);
/** Loop to read our worksheet in "chunk size" blocks **/
/** $startRow is set to 2 initially because we always read the headings in row #1 **/
for ($startRow = 2; $startRow <= 65536; $startRow += $chunkSize) {
/** Tell the Read Filter, the limits on which rows we want to read this iteration **/
$chunkFilter->setRows($startRow,$chunkSize);
/** Load only the rows that match our filter from $inputFileName to a PHPExcel Object **/
$objPHPExcel = $objReader->load($inputFileName);
// Do some processing here
// Free up some of the memory
$objPHPExcel->disconnectWorksheets();
unset($objPHPExcel);
}
我要处理尽可能多的行有数据.我尝试使用Worksheet对象中的方法getHighestRow(),但是它只是一直返回A1
I want to process as many rows have data. I tried using the method getHighestRow() from the Worksheet object, but it just kept returning A1
我还尝试通过编写以下小函数来检查下一个检索到的行是否为空:
I also tried to check if the next retrieved row was empty by writing this little function:
function _emptyRow($row) {
$empty=true;
foreach($row as $r) {
if ($r!='') {
$empty = false;
}
}
return $empty;
}
因此,如果_emptyRow(),我将break
退出循环.这对我没有用.
So if _emptyRow() I will break
out of the loop . This didnt work for me.
有人能建议一种只检索所有有数据记录的方法吗?当我运行它时,即使只有大约1500行有数据,在超时之前它也会达到23000(使用set_time_limit(240));
Can anyone suggest a way to only retrieve all the records which have data? When I run this it even though only about 1500 rows have data it gets to about 23000 before timing out ( with set_time_limit(240));
推荐答案
我通常会这样做:
$objPHPExcel = $objReader->load($inputFileName);
$rows = count($objPHPExcel->getActiveSheet()->toArray());
for ($start_row = 1; $start_row < $rows; $start_row ++)
// ...
$excel->getActiveSheet()->toArray()
将仅返回数组中的每一行(包含数据).
$excel->getActiveSheet()->toArray()
will only return each row (with data) in an array.
这篇关于如何在块循环中使用PHPExcel库确定文件的结尾?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!