要使用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的單元格將被合并,并且具有指定的樣式。