在使用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;
}
文章來源:劉俊濤的博客 歡迎關注公眾號、留言、評論,一起學習。
__________________________________________________________________________________
若有幫助到您,歡迎捐贈支持,您的支持是對我堅持最好的肯定(*^_^*)
