dongshi1868 2012-04-02 07:29
浏览 85

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

Using the PHPExcel library, I am attempting to iterate over around 1500 rows, each row has about 25 columns.

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); 

}

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;

}

So if _emptyRow() I will break out of the loop . This didnt work for me.

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));

  • 写回答

2条回答 默认 最新

  • dongying195959 2012-04-02 07:36
    关注

    I would normally do this:

    $objPHPExcel = $objReader->load($inputFileName);    
    $rows = count($objPHPExcel->getActiveSheet()->toArray());    
    for ($start_row = 1; $start_row < $rows; $start_row ++)
    // ...
    

    $excel->getActiveSheet()->toArray() will only return each row (with data) in an array.

    评论

报告相同问题?

悬赏问题

  • ¥15 yolov8边框坐标
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂