在使用PhpSpreadsheet出现空行导入的问题,项目一直报错,原来是读取excel后会多读取几行,或者说读取的行数有错误。因此必须加一个,去除空行。暂时没有研究源码可能源码里面有这样的功能。暂时手动去除空行。
Phpexcel 导入excel 去除空行
相关代码:
public static function excelToArray($filePath)
{
$spreadsheet = IOFactory::load($filePath); // 载入excel表格
$worksheet = $spreadsheet->getActiveSheet();
$highestRow = $worksheet->getHighestRow(); // 总行数
$highestColumn = $worksheet->getHighestColumn(); // 总列数
$highestColumnIndex = Coordinate::columnIndexFromString($highestColumn);
$data = [];
for ($row = 2; $row <= $highestRow; ++$row) { // 从第二行开始
$i = 0;
$row_data = [];
for ($column = 1; $column <= $highestColumnIndex; $column++) {
$row_data[] = $worksheet->getCellByColumnAndRow($column, $row)->getValue();
$i++;
}
if(!implode('',$row_data)){
//删除空行
continue;
}
$data[] = $row_data;
}
return $data;
}
文章来源:刘俊涛的博客 欢迎关注公众号、留言、评论,一起学习。
__________________________________________________________________________________
若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(*^_^*)