Skip to main content

Overview

The Alignment class controls text alignment, rotation, wrapping, and indentation within cells.

Namespace

PhpOffice\PhpSpreadsheet\Style\Alignment

Getting an Alignment Object

// Via Style object
$alignment = $spreadsheet->getActiveSheet()->getStyle('A1')->getAlignment();

// Via chaining
$spreadsheet->getActiveSheet()->getStyle('A1')
    ->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_CENTER)
    ->setVertical(Alignment::VERTICAL_CENTER);

Constants

Horizontal Alignment

HORIZONTAL_GENERAL
string
default:"'general'"
General alignment (default)
HORIZONTAL_LEFT
string
default:"'left'"
Left-aligned
HORIZONTAL_CENTER
string
default:"'center'"
Center-aligned
HORIZONTAL_RIGHT
string
default:"'right'"
Right-aligned
HORIZONTAL_JUSTIFY
string
default:"'justify'"
Justified alignment
HORIZONTAL_FILL
string
default:"'fill'"
Fill alignment (repeats content to fill cell width)
HORIZONTAL_CENTER_CONTINUOUS
string
default:"'centerContinuous'"
Center across selection
HORIZONTAL_DISTRIBUTED
string
default:"'distributed'"
Distributed alignment (Excel 2007+)

Vertical Alignment

VERTICAL_TOP
string
default:"'top'"
Top-aligned
VERTICAL_CENTER
string
default:"'center'"
Center-aligned
VERTICAL_BOTTOM
string
default:"'bottom'"
Bottom-aligned (default)
VERTICAL_JUSTIFY
string
default:"'justify'"
Justified alignment
VERTICAL_DISTRIBUTED
string
default:"'distributed'"
Distributed alignment (Excel 2007+)

Read Order

READORDER_CONTEXT
int
default:"0"
Context-dependent reading order
READORDER_LTR
int
default:"1"
Left-to-right reading order
READORDER_RTL
int
default:"2"
Right-to-left reading order

Text Rotation

TEXTROTATION_STACK_PHPSPREADSHEET
int
default:"-165"
Stack text vertically (special value for PhpSpreadsheet)
TEXTROTATION_STACK_EXCEL
int
default:"255"
Stack text vertically (Excel format value)

Properties

horizontal
string
default:"'general'"
Horizontal alignment
vertical
string
default:"'bottom'"
Vertical alignment
textRotation
int
default:"0"
Text rotation angle (-90 to 90 degrees, or -165 for vertical stacking)
wrapText
bool
default:"false"
Whether to wrap text in cell
shrinkToFit
bool
default:"false"
Whether to shrink text to fit cell
indent
int
default:"0"
Indentation level (only works with left/right/distributed horizontal alignment)
readOrder
int
default:"0"
Reading order (0=context, 1=LTR, 2=RTL)
justifyLastLine
bool
default:"null"
Whether to justify the last line (used with justify alignment)

Methods

getHorizontal()

Get horizontal alignment.
public function getHorizontal(): ?string

setHorizontal()

Set horizontal alignment.
public function setHorizontal(string $horizontalAlignment): static
horizontalAlignment
string
required
Horizontal alignment (use HORIZONTAL_* constants)
Example:
$alignment->setHorizontal(Alignment::HORIZONTAL_CENTER);
$alignment->setHorizontal(Alignment::HORIZONTAL_LEFT);
$alignment->setHorizontal(Alignment::HORIZONTAL_RIGHT);

getVertical()

Get vertical alignment.
public function getVertical(): ?string

setVertical()

Set vertical alignment.
public function setVertical(string $verticalAlignment): static
verticalAlignment
string
required
Vertical alignment (use VERTICAL_* constants)
Example:
$alignment->setVertical(Alignment::VERTICAL_CENTER);
$alignment->setVertical(Alignment::VERTICAL_TOP);
$alignment->setVertical(Alignment::VERTICAL_BOTTOM);

getTextRotation()

Get text rotation angle.
public function getTextRotation(): ?int

setTextRotation()

Set text rotation angle.
public function setTextRotation(int $angleInDegrees): static
angleInDegrees
int
required
Rotation angle in degrees (-90 to 90, or -165 for vertical stacking)
Example:
$alignment->setTextRotation(45);   // 45 degrees
$alignment->setTextRotation(-45);  // -45 degrees
$alignment->setTextRotation(90);   // 90 degrees
$alignment->setTextRotation(-165); // Vertical stacking

getWrapText()

Get wrap text setting.
public function getWrapText(): bool

setWrapText()

Set wrap text.
public function setWrapText(bool $wrapped): static
wrapped
bool
required
Whether to wrap text
Example:
$alignment->setWrapText(true);

getShrinkToFit()

Get shrink to fit setting.
public function getShrinkToFit(): bool

setShrinkToFit()

Set shrink to fit.
public function setShrinkToFit(bool $shrink): static
shrink
bool
required
Whether to shrink text to fit cell
Example:
$alignment->setShrinkToFit(true);

getIndent()

Get indentation level.
public function getIndent(): int

setIndent()

Set indentation level.
public function setIndent(int $indent): static
indent
int
required
Indentation level (only works with left, right, or distributed alignment)
Example:
$alignment->setHorizontal(Alignment::HORIZONTAL_LEFT)
          ->setIndent(2);

getReadOrder()

Get reading order.
public function getReadOrder(): int

setReadOrder()

Set reading order.
public function setReadOrder(int $readOrder): static
readOrder
int
required
Reading order (use READORDER_* constants)
Example:
$alignment->setReadOrder(Alignment::READORDER_RTL); // Right-to-left
$alignment->setReadOrder(Alignment::READORDER_LTR); // Left-to-right

getJustifyLastLine()

Get justify last line setting.
public function getJustifyLastLine(): ?bool

setJustifyLastLine()

Set justify last line.
public function setJustifyLastLine(bool $justifyLastLine): static
justifyLastLine
bool
required
Whether to justify the last line

applyFromArray()

Apply multiple alignment settings at once.
public function applyFromArray(array $styleArray): static
styleArray
array
required
Array containing alignment style information
Example:
$alignment->applyFromArray([
    'horizontal' => Alignment::HORIZONTAL_CENTER,
    'vertical' => Alignment::VERTICAL_CENTER,
    'textRotation' => 0,
    'wrapText' => true
]);

Complete Examples

Basic Alignment

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Center alignment (both horizontal and vertical)
$sheet->getStyle('A1')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_CENTER)
    ->setVertical(Alignment::VERTICAL_CENTER);

// Left-aligned
$sheet->getStyle('A2')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT);

// Right-aligned
$sheet->getStyle('A3')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_RIGHT);

Text Wrapping

// Enable text wrapping for long text
$sheet->setCellValue('A1', 'This is a very long text that should wrap within the cell');
$sheet->getStyle('A1')->getAlignment()->setWrapText(true);

// Or using applyFromArray
$sheet->getStyle('A2')->applyFromArray([
    'alignment' => [
        'wrapText' => true
    ]
]);

Text Rotation

// 45 degree rotation
$sheet->getStyle('A1')->getAlignment()->setTextRotation(45);

// 90 degree rotation (vertical text)
$sheet->getStyle('A2')->getAlignment()->setTextRotation(90);

// -90 degree rotation
$sheet->getStyle('A3')->getAlignment()->setTextRotation(-90);

// Vertical stacking
$sheet->getStyle('A4')->getAlignment()->setTextRotation(-165);

Shrink to Fit

// Text will automatically shrink to fit cell width
$sheet->setCellValue('A1', 'Very long text that needs to fit');
$sheet->getStyle('A1')->getAlignment()->setShrinkToFit(true);

Indentation

// Indent text (only works with left/right alignment)
$sheet->getStyle('A1')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT)
    ->setIndent(2);

$sheet->getStyle('A2')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT)
    ->setIndent(4);

Header Row Styling

// Center-aligned, bold header
$sheet->getStyle('A1:E1')->applyFromArray([
    'font' => [
        'bold' => true
    ],
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_CENTER,
        'vertical' => Alignment::VERTICAL_CENTER
    ]
]);

Table with Mixed Alignment

// Headers - centered
$sheet->getStyle('A1:D1')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_CENTER);

// Text columns - left aligned
$sheet->getStyle('A2:B100')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT);

// Number columns - right aligned
$sheet->getStyle('C2:D100')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_RIGHT);

Multi-line Text

// Text with line breaks
$sheet->setCellValue('A1', "Line 1\nLine 2\nLine 3");
$sheet->getStyle('A1')->getAlignment()
    ->setWrapText(true)
    ->setVertical(Alignment::VERTICAL_TOP);

Justified Text

$sheet->setCellValue('A1', 'This is a long paragraph that will be justified across the cell width.');
$sheet->getStyle('A1')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_JUSTIFY)
    ->setWrapText(true);

Center Across Selection

// Center text across multiple cells without merging
$sheet->setCellValue('A1', 'Centered Across');
$sheet->getStyle('A1:E1')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_CENTER_CONTINUOUS);

Vertical Header

// Create vertical column headers
$headers = ['Jan', 'Feb', 'Mar', 'Apr', 'May'];
foreach ($headers as $col => $header) {
    $columnLetter = \PhpOffice\PhpSpreadsheet\Cell\Coordinate::stringFromColumnIndex($col + 1);
    $sheet->setCellValue($columnLetter . '1', $header);
    $sheet->getStyle($columnLetter . '1')->getAlignment()
        ->setTextRotation(90)
        ->setHorizontal(Alignment::HORIZONTAL_CENTER);
}

Right-to-Left Text

// For RTL languages (Arabic, Hebrew, etc.)
$sheet->getStyle('A1')->getAlignment()
    ->setReadOrder(Alignment::READORDER_RTL)
    ->setHorizontal(Alignment::HORIZONTAL_RIGHT);

Complete Table Example

$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();

// Headers
$sheet->setCellValue('A1', 'Name');
$sheet->setCellValue('B1', 'Age');
$sheet->setCellValue('C1', 'Salary');
$sheet->setCellValue('D1', 'Notes');

// Header styling
$sheet->getStyle('A1:D1')->applyFromArray([
    'font' => ['bold' => true],
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_CENTER,
        'vertical' => Alignment::VERTICAL_CENTER
    ]
]);

// Data alignment
$sheet->getStyle('A2:A100')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT);  // Names

$sheet->getStyle('B2:C100')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_RIGHT); // Numbers

$sheet->getStyle('D2:D100')->getAlignment()
    ->setHorizontal(Alignment::HORIZONTAL_LEFT)
    ->setWrapText(true);                          // Notes with wrapping

Common Use Cases

Table Header

$sheet->getStyle('A1:E1')->applyFromArray([
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_CENTER,
        'vertical' => Alignment::VERTICAL_CENTER
    ]
]);

Number Column

$sheet->getStyle('C:C')->applyFromArray([
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_RIGHT
    ]
]);

Text Column

$sheet->getStyle('A:A')->applyFromArray([
    'alignment' => [
        'horizontal' => Alignment::HORIZONTAL_LEFT,
        'wrapText' => true
    ]
]);

See Also

Build docs developers (and LLMs) love