phpexcel中遇到空单元格不跳过
之前用phpexcel类,代码是这样写的:
/* 导入excel文件 */
$fileType = PHPExcel_IOFactory::identify($destination); //自动获取文件的类型提供给phpexcel用
$objReader = PHPExcel_IOFactory::createReader($fileType); //获取文件读取操作对象
$objPHPExcel = $objReader->load($destination); //加载文件
foreach ($objPHPExcel->getWorksheetIterator() as $sheet) {//循环取sheet
foreach ($sheet->getRowIterator() as $row) {//逐行处理
/*逐列读取*/
$cellIterator = $row->getCellIterator();
foreach ($cellIterator as $cell) {//逐列读取
$data = $cell->getValue();//获取值
}
}
}
这样写的时候,如果每一列单元格都没有为空,是没问题的,但是如果为空的话,就会跳过为空的单元格读取下一列的数值,导致最后数据出错。
修改后代码如下:
/*逐列读取*/
$cellIterator = $row->getCellIterator();
/*遇空单元格不跳过*/
$cellIterator->setIterateOnlyExistingCells(false);
foreach ($cellIterator as $cell) {//逐列读取
$data = $cell->getValue();//获取值
}
可以动手试一下。
该日志未加标签。