Remove empty rows in PHPexcel

时间:2018-01-15 18:10:33

标签: php phpexcel

I have the following code .

$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestDataRow(); 
$highestColumn = $sheet->getHighestDataColumn();
for ($row = 2; $row <= $highestRow; $row++){ 
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . 
       $row,NULL,TRUE,FALSE);

}

I need to ignore rows if all columns are empty.I have seen links ignore empty columns ,but am not sure about this will work for me. I dont need to eliminate columns if it is empty,but I need to eliminate entire row,if all the columns are empty.

2 个答案:

答案 0 :(得分:3)

您需要迭代行并检查所有单元格是否为空

示例代码

$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestDataRow(); 
$highestColumn = $sheet->getHighestDataColumn();
for ($row = 2; $row <= $highestRow; $row++){ 
    $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,NULL,TRUE,FALSE);
    if(isEmptyRow(reset($rowData))) { continue; } // skip empty row
    // do something usefull
}

function isEmptyRow($row) {
    foreach($row as $cell){
        if (null !== $cell) return false;
    }
    return true;
}

答案 1 :(得分:0)

这是我使用的代码,该代码段仅获取插入的数据区域

谢谢

$inputfiletype = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputfiletype);
$objReader->setReadDataOnly(true);//Get the area of data inserted
$objPHPExcel = $objReader->load($inputFileName);




$sheet = $objPHPExcel->getSheet(0); 
$highestRow = $sheet->getHighestDataRow(); 
$highestColumn = $sheet->getHighestColumn();