Laravel中用PhpSpreadsheet导出Excel报「Invalid cell coordinate」错误
Laravel中使用PhpSpreadsheet导出Excel报「Invalid cell coordinate 01」错误的解决方法
问题描述
在Laravel项目中使用PhpSpreadsheet导出Excel报表时,出现「Invalid cell coordinate 01」错误。已确认setCellValueByColumnAndRow方法使用了整数列索引,但问题仍未解决。
错误原因分析
- 表头单元格坐标拼接错误:遍历表头数组时,直接使用数组的0-based索引(如0、1、2...)拼接成
"{$column}{$row}"格式的坐标(例如第一个单元格变成"01"),但PhpSpreadsheet不支持纯数字列标识的单元格坐标,此类坐标无法被解析为有效的Excel单元格位置。 - 重复实例化Spreadsheet对象:代码中先后两次实例化
Spreadsheet,导致$activeSheet指向第一个实例,而后续保存用的是第二个实例,逻辑混乱且可能引发异常。 - 变量名混淆:同时存在
$activeSheet和$active_sheet两个变量,容易导致操作对象错误。
解决方案
- 表头写入改用
setCellValueByColumnAndRow方法,将数组的0-based索引转换为1-based的列索引(PhpSpreadsheet列索引从1开始)。 - 仅实例化一次
Spreadsheet对象,统一操作同一个工作表实例。 - 统一变量名,避免混淆。
修正后的控制器代码
public function getFestivalPlanRegister($id, $author = null) { if (request()->has('excel_export')) { $festival = $festival->where('festival_plan.fsp_condition_plan', '=', 'completed'); } if (request()->has('excel_export')) { // 仅实例化一次Spreadsheet对象 $spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet(); $activeSheet = $spreadsheet->getActiveSheet(); $activeSheet->setRightToLeft(true); $style = [ 'alignment' => ['horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER], ]; // 定义表头字段 $headerColumns = [ 'Registration ID', 'Project Tracking Code', 'Farsi Title of the Project', 'English Title of the Project', 'Section', 'Axis', 'Challenge', 'Lead Author Name', 'Research Location', 'Lead Author Contact Number', 'Lead Author Email', 'Lead Author Address', 'Lead Author Postal Code', 'Lead Author Province', 'Lead Author City', 'Lead Author Academic Level', 'Lead Author Educational Level', 'Project Status', 'Collaborator Name', 'Collaborator National ID', 'Collaborator Academic Level', 'Collaborator Educational Level', 'Collaborator Province', 'Collaborator City', 'Collaborator Educational Institution', 'Supervisor Name', 'Supervisor Contact Number', 'Supervisor Email', 'Supervisor National ID', 'Supervisor Address', 'Supervisor Postal Code', 'Supervisor Academic Level', 'Supervisor Field of Study', 'Supervisor Province', 'Supervisor City', 'First Stage Average Score', 'Second Stage Average Score', 'Third Stage Average Score', 'Status' ]; $row = 1; // 写入表头:将数组0-based索引转为1-based列索引 foreach ($headerColumns as $index => $header) { $columnIndex = $index + 1; $activeSheet->setCellValueByColumnAndRow($columnIndex, $row, $header); $activeSheet->getStyleByColumnAndRow($columnIndex, $row)->applyFromArray($style); } $row = 2; foreach ($festival as $item) { $activeSheet->setCellValueByColumnAndRow(1, $row, $item->fsp_code); $activeSheet->getStyleByColumnAndRow(1, $row)->applyFromArray($style); $activeSheet->setCellValueByColumnAndRow(2, $row, $item->fsp_id); $activeSheet->getStyleByColumnAndRow(2, $row)->applyFromArray($style); // 其他字段写入逻辑... $row++; } $filename = 'festival_report.xlsx'; header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); header("Content-Disposition: attachment;filename=\"$filename\""); header('Cache-Control: max-age=0'); $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet); $writer->save('php://output'); die(); } return $festival; }
关键说明
- PhpSpreadsheet的
setCellValueByColumnAndRow方法中,列索引为1-based(第一列对应数字1,即Excel的A列),因此需要将数组的0-based索引加1转换。 - 确保所有工作表操作都指向同一个
$activeSheet实例,避免重复实例化导致的对象不一致问题。
内容的提问来源于stack exchange,提问作者Pouya Vaghefi
相关产品推荐
相关产品推荐

