Skip to main content

Overview

The Font class controls all font-related styling including font family, size, bold, italic, underline, strikethrough, color, and text effects.

Namespace

PhpOffice\PhpSpreadsheet\Style\Font

Getting a Font Object

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

// Via chaining
$spreadsheet->getActiveSheet()->getStyle('A1')
    ->getFont()
    ->setBold(true)
    ->setSize(14);

Constants

Underline Types

UNDERLINE_NONE
string
default:"'none'"
No underline
UNDERLINE_SINGLE
string
default:"'single'"
Single underline
UNDERLINE_DOUBLE
string
default:"'double'"
Double underline
UNDERLINE_SINGLEACCOUNTING
string
default:"'singleAccounting'"
Single accounting underline
UNDERLINE_DOUBLEACCOUNTING
string
default:"'doubleAccounting'"
Double accounting underline

Capitalization

CAP_ALL
string
default:"'all'"
All caps
CAP_SMALL
string
default:"'small'"
Small caps
CAP_NONE
string
default:"'none'"
No capitalization

Default Values

DEFAULT_FONT_NAME
string
default:"'Calibri'"
Default font name

Properties

Basic Properties

name
string
default:"'Calibri'"
Font name/family
size
float
default:"11"
Font size in points
bold
bool
default:"false"
Bold text
italic
bool
default:"false"
Italic text
superscript
bool
default:"false"
Superscript text (mutually exclusive with subscript)
subscript
bool
default:"false"
Subscript text (mutually exclusive with superscript)
underline
string
default:"'none'"
Underline style (use UNDERLINE_* constants)
strikethrough
bool
default:"false"
Strikethrough text
color
Color
Font color object

Methods

getName()

Get the font name.
public function getName(): ?string

setName()

Set the font name.
public function setName(string $fontname): self
fontname
string
required
Font name (e.g., ‘Arial’, ‘Times New Roman’, ‘Courier New’)
Example:
$font->setName('Arial');

getSize()

Get the font size.
public function getSize(): ?float

setSize()

Set the font size in points.
public function setSize(mixed $sizeInPoints, bool $nullOk = false): static
sizeInPoints
float
required
Font size in points (1/72 of an inch). Must be positive.
nullOk
bool
default:"false"
Whether to allow null values
Example:
$font->setSize(14);
$font->setSize(10.5);

getBold()

Get bold setting.
public function getBold(): ?bool

setBold()

Set bold.
public function setBold(bool $bold): static
bold
bool
required
Whether text should be bold
Example:
$font->setBold(true);

getItalic()

Get italic setting.
public function getItalic(): ?bool

setItalic()

Set italic.
public function setItalic(bool $italic): static
italic
bool
required
Whether text should be italic
Example:
$font->setItalic(true);

getSuperscript()

Get superscript setting.
public function getSuperscript(): ?bool

setSuperscript()

Set superscript. Automatically disables subscript.
public function setSuperscript(bool $superscript): static
superscript
bool
required
Whether text should be superscript
Example:
$font->setSuperscript(true);

getSubscript()

Get subscript setting.
public function getSubscript(): ?bool

setSubscript()

Set subscript. Automatically disables superscript.
public function setSubscript(bool $subscript): static
subscript
bool
required
Whether text should be subscript
Example:
$font->setSubscript(true);

getUnderline()

Get underline style.
public function getUnderline(): ?string

setUnderline()

Set underline style.
public function setUnderline(bool|string $underlineStyle): static
underlineStyle
bool|string
required
Underline style. Use UNDERLINE_* constants, or true for UNDERLINE_SINGLE, false for UNDERLINE_NONE
Example:
$font->setUnderline(Font::UNDERLINE_SINGLE);
$font->setUnderline(Font::UNDERLINE_DOUBLE);
$font->setUnderline(true);  // Same as UNDERLINE_SINGLE
$font->setUnderline(false); // Same as UNDERLINE_NONE

getStrikethrough()

Get strikethrough setting.
public function getStrikethrough(): ?bool

setStrikethrough()

Set strikethrough.
public function setStrikethrough(bool $strikethru): static
strikethru
bool
required
Whether text should have strikethrough
Example:
$font->setStrikethrough(true);

getColor()

Get the Color object.
public function getColor(): Color

setColor()

Set font color.
public function setColor(Color $color): static
color
Color
required
Color object
Example:
// Using the Color object
$font->getColor()->setRGB('FF0000');
$font->getColor()->setARGB('FFFF0000');

// Or create a new Color
use PhpOffice\PhpSpreadsheet\Style\Color;
$color = new Color('FF0000');
$font->setColor($color);

applyFromArray()

Apply multiple font settings at once.
public function applyFromArray(array $styleArray): static
styleArray
array
required
Array containing font style information
Example:
$font->applyFromArray([
    'name' => 'Arial',
    'bold' => true,
    'italic' => false,
    'underline' => Font::UNDERLINE_DOUBLE,
    'strikethrough' => false,
    'color' => [
        'rgb' => '808080'
    ],
    'size' => 14
]);

Complete Examples

Basic Font Styling

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Font;

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

// Method 1: Using individual setters
$font = $sheet->getStyle('A1')->getFont();
$font->setName('Arial')
     ->setSize(14)
     ->setBold(true)
     ->setItalic(true)
     ->setUnderline(Font::UNDERLINE_SINGLE)
     ->getColor()->setRGB('FF0000');

// Method 2: Using applyFromArray
$sheet->getStyle('B1')->getFont()->applyFromArray([
    'name' => 'Times New Roman',
    'size' => 12,
    'bold' => false,
    'italic' => true,
    'color' => ['rgb' => '0000FF']
]);

Text Effects

// Superscript (for exponents)
$sheet->setCellValue('A1', 'E=mc²');
$sheet->getStyle('A1')->getFont()->setSuperscript(true);

// Subscript (for chemical formulas)
$sheet->setCellValue('A2', 'H₂O');
$sheet->getStyle('A2')->getFont()->setSubscript(true);

// Strikethrough (for deleted text)
$sheet->setCellValue('A3', 'Removed text');
$sheet->getStyle('A3')->getFont()->setStrikethrough(true);

Header Styling

// Create a bold header row
$headerStyle = [
    'font' => [
        'bold' => true,
        'size' => 12,
        'name' => 'Arial',
        'color' => ['rgb' => 'FFFFFF']
    ],
    'fill' => [
        'fillType' => \PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID,
        'startColor' => ['rgb' => '4472C4']
    ]
];

$sheet->getStyle('A1:E1')->applyFromArray($headerStyle);

Different Font Colors

// Red text
$sheet->getStyle('A1')->getFont()->getColor()->setRGB('FF0000');

// Green text with alpha channel
$sheet->getStyle('A2')->getFont()->getColor()->setARGB('FF00FF00');

// Blue text
$sheet->getStyle('A3')->getFont()->getColor()->setRGB('0000FF');

Combining Multiple Effects

$sheet->getStyle('A1')->getFont()->applyFromArray([
    'name' => 'Courier New',
    'size' => 11,
    'bold' => true,
    'italic' => true,
    'underline' => Font::UNDERLINE_DOUBLE,
    'color' => ['rgb' => '800080']
]);

See Also

Build docs developers (and LLMs) love