Trying to add styling to PHPSpreadsheet’ cells:
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$irow = $icol = 1;
foreach ([
'Operation type',
'Name',
'Status',
'Start',
'End',
'Duration'
] as $title) {
$sheet->setCellValue([$icol, $irow], $title);
$sheet->getStyle([$icol, $irow])->getFont()->setBold(true);
$sheet->getStyle([$icol, $irow])->getAlignment()->setVertical(Alignment::VERTICAL_TOP);
$icol++;
}
//
$writer = new Xlsx($spreadsheet);
$writer->save('hello world.xlsx');
Finally, I see properly filled Excel sheet without any styling – no bold font, no vertical alignment.
What’s wrong?
UPDATE:
getStyleaccepts array of 4 ints instead of array of 2 ints insetCellValue:
/**
* Get style for cell.
*
* @param AddressRange|array<int>|CellAddress|int|string $cellCoordinate
* A simple string containing a cell address like 'A1' or a cell range like 'A1:E10'
* or passing in an array of [$fromColumnIndex, $fromRow, $toColumnIndex, $toRow] (e.g. [3, 5, 6, 8]),
* or a CellAddress or AddressRange object.
*/
public function getStyle($cellCoordinate): Style
getStyleByColumnAndRowmarked as deprecated. Recommends to usegetStyleinstead