0
点赞
收藏
分享

微信扫一扫

在使用PhpSpreadsheet出现空行导入的问题

Raow1 2022-05-09 阅读 100


在使用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;

}

    

__________________________________________________________________________________

若有帮助到您,欢迎捐赠支持,您的支持是对我坚持最好的肯定(*^_^*)

你要保守你心,胜过保守一切。

作者:刘俊涛的博客​


举报

相关推荐

0 条评论