猿问

PHP 电子表格数据填充不会通过“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;


莫回无
浏览 105回答 1
1回答

开心每一天1111

改变foreach (range('A', $spreadsheet->getActiveSheet()->getHighestDataColumn()) as $col) {}&nbsp;至$max_column = $spreadsheet->getActiveSheet()->getHighestDataColumn();for($column = 'A'; !isBiggerExcelColumn($column,$max_column); ++$column){&nbsp; &nbsp; // rest of your code}function isBiggerExcelColumn($current_column,$max_column){&nbsp; &nbsp; if(strlen($current_column) < strlen($max_column)) return false;&nbsp; &nbsp; return strlen($current_column) > strlen($max_column) || strcmp($current_column,$max_column) > 0;}在上面,我们使用++$column转到 PHP 提供的用于递增字符串的下一个 excel 列。在 中isBiggerExcelColumn(),我们使用 amd 和其他长度检查进行字符串比较strcmp,以判断当前列是否大于最大列。如果是,停止循环,否则继续;
随时随地看视频慕课网APP
我要回答