gpt4 book ai didi

php - 如何在 block 循环中使用 PHPExcel 库确定文件的结尾?

转载 作者:行者123 更新时间:2023-12-02 07:42:07 24 4
gpt4 key购买 nike

使用 PHPExcel 库,我试图迭代大约 1500 行,每行大约有 25 列。

我正在使用这段代码(取自 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

我还尝试通过编写这个小函数来检查下一个检索到的行是否为空:

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() 将只返回数组中的每一行(含数据)。

关于php - 如何在 block 循环中使用 PHPExcel 库确定文件的结尾?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9972404/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com