使用 PHPExcel 库,我尝试迭代大约 1500 行,每行大约有 25 列。
我正在使用这段代码(取自PHPExcel 运行内存为 256、512 和 1024MB):
/** 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
我还尝试通过编写这个小函数来检查下一个检索到的行是否为空:
function _emptyRow($row) {
$empty=true;
foreach($row as $r) {
if ($r!='') {
$empty = false;
}
}
return $empty;
}
所以如果 _emptyRow() 我会break
跳出循环 。这对我不起作用。
任何人都可以建议一种方法来仅检索所有有数据的记录吗?当我运行这个程序时,尽管只有大约 1500 行有数据,但在超时之前它会达到大约 23000 行(使用 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()
只会返回数组中的每一行(带有数据)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)