修改通用下载中所有表单为文本格式

This commit is contained in:
2025-09-04 07:19:17 +08:00
parent 68fdd380c8
commit b9f770b21d
1 changed files with 19 additions and 2 deletions

View File

@ -110,13 +110,30 @@ class Excel extends Controller
$columnCount = count($data[0]); $columnCount = count($data[0]);
$rowCount = count($data); $rowCount = count($data);
// 写入数据 // // 写入数据
// foreach ($data as $rowIndex => $rowData) {
// for ($colIndex = 0; $colIndex < $columnCount; $colIndex++) {
// $sheet->setCellValueByColumnAndRow($colIndex + 1, $rowIndex + 1, $rowData[$colIndex]);
// }
// }
// 修改后的代码(强制文本格式)
use PhpOffice\PhpSpreadsheet\Cell\DataType; // 确保顶部已引入
foreach ($data as $rowIndex => $rowData) { foreach ($data as $rowIndex => $rowData) {
for ($colIndex = 0; $colIndex < $columnCount; $colIndex++) { for ($colIndex = 0; $colIndex < $columnCount; $colIndex++) {
$sheet->setCellValueByColumnAndRow($colIndex + 1, $rowIndex + 1, $rowData[$colIndex]); $cellValue = $rowData[$colIndex];
// 强制设置为文本格式
$sheet->setCellValueExplicitByColumnAndRow(
$colIndex + 1,
$rowIndex + 1,
(string)$cellValue,
DataType::TYPE_STRING
);
} }
} }
// 设置表头样式 // 设置表头样式
$headerRange = 'A1:' . $this->getExcelColumnName($columnCount) . '1'; $headerRange = 'A1:' . $this->getExcelColumnName($columnCount) . '1';
$sheet->getStyle($headerRange)->applyFromArray($options['header_style']); $sheet->getStyle($headerRange)->applyFromArray($options['header_style']);