我正在使用 PHPSpreadsheet 创建和 excel 文件当我填充我的数据时它不会传递列 'Z' 当对于此查询我的数据实际上有 81 列时。下面是我的代码:$spreadsheet = new Spreadsheet();$sheet = $spreadsheet->getActiveSheet();$sheet->getDefaultColumnDimension()->setWidth(19);$sheet->getStyle('A:ZZ')->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_LEFT);$sheet->getStyle('A:ZZ')->getAlignment()->setVertical(\PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER);// $sheet->getStyle('A:U')->getAlignment()->setWrapText(true);$sheet->getStyle('A1:ZZ1')->applyFromArray($headerStyleArray);$sheet->getStyle('A:ZZ')->applyFromArray($fontArray);// SET EXCEL HEADER &COLUMN WIDTH TO AUTOSIZE$i=0;foreach (range('A', $spreadsheet->getActiveSheet()->getHighestDataColumn()) as $col) { $spreadsheet->getActiveSheet() ->getColumnDimension($col) ->setAutoSize(true); $sheet->getCell($col.'1')->setValue($header[$i]); if($i >= count($header)-1) { break; } $i++;} // SET EXCEL CELL DATA$rowNumber = 2;foreach($rows as $row) { $i = 0; foreach (range('A', $spreadsheet->getActiveSheet()->getHighestDataColumn()) as $char) { return response()->json($char.$rowNumber); if($i <= count($row)-1) { $sheet->getCell($char.$rowNumber)->setValue($row[$i]); } $i++; } $rowNumber++;}$writer = new Xlsx($spreadsheet);header('Content-Type: application/vnd.ms-excel');header('Content-Disposition: attachment;filename="'. $request->group_name. ' Member Attendance Report.xlsx"');header('Cache-Control: max-age=0');$writer->save('php://output');return;
1 回答
开心每一天1111
TA贡献1836条经验 获得超13个赞
改变
foreach (range('A', $spreadsheet->getActiveSheet()->getHighestDataColumn()) as $col) {
}
至
$max_column = $spreadsheet->getActiveSheet()->getHighestDataColumn();
for($column = 'A'; !isBiggerExcelColumn($column,$max_column); ++$column){
// rest of your code
}
function isBiggerExcelColumn($current_column,$max_column){
if(strlen($current_column) < strlen($max_column)) return false;
return strlen($current_column) > strlen($max_column) || strcmp($current_column,$max_column) > 0;
}
在上面,我们使用++$column转到 PHP 提供的用于递增字符串的下一个 excel 列。在 中isBiggerExcelColumn(),我们使用 amd 和其他长度检查进行字符串比较strcmp,以判断当前列是否大于最大列。如果是,停止循环,否则继续;
- 1 回答
- 0 关注
- 104 浏览
添加回答
举报
0/150
提交
取消