PHP 电子表格数据填充不会通过列 "Z"
PHP Spreadsheet data population wont pass column "Z"
我正在使用 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;
非常感谢任何帮助,谢谢
改变
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()
中,我们使用 strcmp
和其他长度检查来进行字符串比较,以判断当前列是否大于最大列。如果是,停止循环,否则继续;
我正在使用 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;
非常感谢任何帮助,谢谢
改变
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()
中,我们使用 strcmp
和其他长度检查来进行字符串比较,以判断当前列是否大于最大列。如果是,停止循环,否则继续;