gpt4 book ai didi

PhpOffice\PhpSpreadsheet 无法为列或整个电子表格设置默认格式

转载 作者:行者123 更新时间:2023-12-04 16:29:06 30 4
gpt4 key购买 nike

我一直在尝试设置特定列或整个电子表格的默认格式。我正在使用 PhpOffice\PhpSpreadsheet 包来创建 Excel 电子表格。有没有办法一次设置整张或整列的格式?

以下是我如何使用该库的代码片段:

require_once 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
// Create new Spreadsheet object
$spreadsheet = new Spreadsheet();
// Set document properties
$spreadsheet->getProperties()->setCreator('Someone')
->setLastModifiedBy('Someone Else')
->setTitle('XLSX Test Document')
->setSubject('Office 2007 XLSX Test Document')
->setDescription('PhpOffice')
->setKeywords('PhpOffice')
->setCategory('PhpOffice');

$header = [
"Order No",
"ID",
"Salutaion",
"First Name",
"Last Name",
"Address"
];
$rows = [
[
"SDKJFBHIDUF",
"23123156478978945",
"Mr.",
"John",
"Doe",
"101, Lorem Ipsum"
]
];

$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Orders');
$sheet->fromArray($header, null, 'A1');
$sheet->fromArray($rows, null, 'A2');
$spreadsheet->setActiveSheetIndex(0);

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="samplemultitab-invoice-' . time() . '.xlsx"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header('Pragma: public'); // HTTP/1.0
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
$writer->save('php://output');
exit;

现在我正在修改 /PhpSpreadsheet/Worksheet/Worksheet.php 文件 fromArray 方法如下

$this->getCell($currentColumn . $startRow)->setValue($cellValue);

$this->getCell($currentColumn . $startRow)->setValueExplicit($cellValue,\PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);

但我需要一个有效的方法来修复这个通用选项设置。

最佳答案

我遇到了类似的问题,大型 ID 字符串(例如 1234567890123456789)被自动转换为数字并在我的电子表格中显示为 1.23456789012346E+018。

当使用 getStyle($columnIndex)->getNumberFormat()->setFormatCode() 时,您必须使用 PhpOffice\PhpSpreadsheet\Style\NumberFormat 类中的值,尽管存在“FORMAT_TEXT”值,似乎为时已晚,因为单元格值已经被视为数字(Cell\DataType::TYPE_NUMERIC),而不是纯字符串。

根据我自己的经验,我认为唯一(且繁琐)的解决方案是遍历给定列的单元格以使用 setValueExplicit() 方法。

$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();
// Fill the sheet with values using fromArray() method
$spreadsheet->getActiveSheet()->fromArray($dataToFillTheSheet, NULL, 'A1');
// Get the highest row index
$highestRow = $spreadsheet->getActiveSheet()->getHighestRow();
for($i = 1; $i <= $highestRow; $i++) {
$cell = $spreadsheet->getActiveSheet()->getCellByColumnAndRow($columnIndex, $i);
$cell->setValueExplicit($cell->getValue(), \PhpOffice\PhpSpreadsheet\Cell\DataType::TYPE_STRING);
}

不过,我很想知道是否有人知道更好的解决方案。

关于PhpOffice\PhpSpreadsheet 无法为列或整个电子表格设置默认格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55634595/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com