要使用PhpSpreadsheet库合并单元格,请按照以下步骤操作:
composer require phpoffice/phpspreadsheet
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$worksheet = $spreadsheet->getActiveSheet();
$worksheet->mergeCells('A1:B2');
你可以根据需要更改合并区域(例如,‘A1:C3’)和合并类型(例如,‘mergeCells’、'mergeAndCenter’等)。更多关于合并单元格的信息,可以参考PhpSpreadsheet官方文档: https://phpspreadsheet.readthedocs.io/en/latest/topics/cells/merging.html
$styleArray = [
'font' => [
'bold' => true,
],
];
$worksheet->getStyle('A1:B2')->applyFromArray($styleArray);
$writer = new Xlsx($spreadsheet);
$writer->save('example.xlsx');
完成以上步骤后,A1到B2的单元格将被合并,并且具有指定的样式。