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_SINGLEACCOUNTING
string
default:"'singleAccounting'"
Single accounting underline
UNDERLINE_DOUBLEACCOUNTING
string
default:"'doubleAccounting'"
Double accounting underline
Capitalization
Default Values
DEFAULT_FONT_NAME
string
default:"'Calibri'"
Default font name
Properties
Basic Properties
name
string
default:"'Calibri'"
Font name/family
Superscript text (mutually exclusive with subscript)
Subscript text (mutually exclusive with superscript)
Underline style (use UNDERLINE_* constants)
Methods
getName()
Get the font name.
public function getName(): ?string
setName()
Set the font name.
public function setName(string $fontname): self
Font name (e.g., ‘Arial’, ‘Times New Roman’, ‘Courier New’)
Example:
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
Font size in points (1/72 of an inch). Must be positive.
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
Whether text should be bold
Example:
getItalic()
Get italic setting.
public function getItalic(): ?bool
setItalic()
Set italic.
public function setItalic(bool $italic): static
Whether text should be italic
Example:
getSuperscript()
Get superscript setting.
public function getSuperscript(): ?bool
setSuperscript()
Set superscript. Automatically disables subscript.
public function setSuperscript(bool $superscript): static
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
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
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
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
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
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);
// 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