Beyond the style array way you can also do it in the method chaining way:
$spreadsheet->getActiveSheet()->getStyle($cells)->getAlignment()->setHorizontal($align)$spreadsheet->getActiveSheet()->getStyle($cells)->getAlignment()->setVertical($align);
$cells
should be a single cell ('A1'
) or a range of cells ('A1:E4'
).$align
should be a constant of the\PhpOffice\PhpSpreadsheet\Style\Alignment
class (or its string value) for the desired alignment.- for horizontal alignment:
Alignment::HORIZONTAL_GENERAL
or'general'
Alignment::HORIZONTAL_LEFT
or'left'
Alignment::HORIZONTAL_RIGHT
or'right'
Alignment::HORIZONTAL_CENTER
or'center'
Alignment::HORIZONTAL_CENTER_CONTINUOUS
or'centerContinuous'
Alignment::HORIZONTAL_JUSTIFY
or'justify'
Alignment::HORIZONTAL_FILL
or'fill'
Alignment::HORIZONTAL_DISTRIBUTED
or'distributed'
(Excel2007 only)
- for vertical alignment:
Alignment::VERTICAL_BOTTOM
or'bottom'
Alignment::VERTICAL_TOP
or'top'
Alignment::VERTICAL_CENTER
or'center'
Alignment::VERTICAL_JUSTIFY
or'justify'
Alignment::VERTICAL_DISTRIBUTED
or'distributed'
(Excel2007 only)
- for horizontal alignment:
The source of these is the source.