PHPOffice/PhpSpreadsheet · error · PhpOffice\PhpSpreadsheet\Exception
No default style found for this workbook
Error message
No default style found for this workbook
What it means
The workbook's default style is simply cellXfCollection[0]; getDefaultStyle() returns it or throws 'No default style found for this workbook' when that collection is empty. An empty cellXf collection means the workbook was constructed or mutated into an inconsistent state (a normal Spreadsheet always registers an initial xf), so in practice this indicates broken internals or manual tampering rather than an API misuse.
Source
Thrown at src/PhpSpreadsheet/Spreadsheet.php:1316
/**
* Check if style exists in style collection.
*/
public function cellXfExists(Style $cellStyleIndex): bool
{
return in_array($cellStyleIndex, $this->cellXfCollection, true);
}
/**
* Get default style.
*/
public function getDefaultStyle(): Style
{
if (isset($this->cellXfCollection[0])) {
return $this->cellXfCollection[0];
}
throw new Exception('No default style found for this workbook');
}
/**
* Add a cellXf to the workbook.
*/
public function addCellXf(Style $style): void
{
$this->cellXfCollection[] = $style;
$style->setIndex(count($this->cellXfCollection) - 1);
}
/**
* Remove cellXf by index. It is ensured that all cells get their xf index updated.
*
* @param int $cellStyleIndex Index to cellXf
*/
public function removeCellXfByIndex(int $cellStyleIndex): void
{View on GitHub (pinned to 65b080eef4)
Solutions
- Do not strip all cellXfs: keep index 0, or rely on garbageCollect() which preserves referenced styles correctly.
- Re-register a default: $spreadsheet->addCellXf(new \PhpOffice\PhpSpreadsheet\Style\Style()); then getDefaultStyle() works again.
- If the object arrived from cache/unserialize, rebuild the Spreadsheet from its source file instead.
- Use the public style API ($sheet->getStyle('A1')->applyFromArray([...])) instead of touching the xf collections.
Example fix
// before
$style = $spreadsheet->getDefaultStyle(); // throws when cellXfCollection is empty
// after
if (count($spreadsheet->getCellXfCollection()) === 0) {
$spreadsheet->addCellXf(new \PhpOffice\PhpSpreadsheet\Style\Style());
}
$style = $spreadsheet->getDefaultStyle(); Defensive patterns
Strategy: validation
Validate before calling
if (count($spreadsheet->getCellXfCollection()) === 0) {
$spreadsheet->addCellXf(new \PhpOffice\PhpSpreadsheet\Style\Style());
}
$default = $spreadsheet->getDefaultStyle(); Type guard
function hasDefaultStyle(\PhpOffice\PhpSpreadsheet\Spreadsheet $s): bool
{
return count($s->getCellXfCollection()) > 0;
} Try / catch
try {
$style = $spreadsheet->getDefaultStyle();
} catch (\PhpOffice\PhpSpreadsheet\Exception $e) {
$spreadsheet->addCellXf(new \PhpOffice\PhpSpreadsheet\Style\Style());
$style = $spreadsheet->getDefaultStyle();
} Prevention
- Never strip the whole cellXf collection; keep index 0.
- Use garbageCollect() instead of manual xf removal.
- Rebuild spreadsheets from source files rather than reviving cached objects.
When it happens
Trigger: Calling getDefaultStyle() after code emptied the cellXf collection (misuse of removeCellXfByIndex on index 0, or direct manipulation of internals via reflection/serialization tricks); on a Spreadsheet object reconstructed badly from an unserialize payload; very rarely from corrupted reader output.
Common situations: Custom style-pruning code that removes xfs one by one including index 0; exotic caching/serialization pipelines that resurrect half-initialized Spreadsheet objects; forks or forks-of-logic that bypass the constructor's default style registration.
Related errors
- CellXf index is out of bounds.
- CellStyleXf index is out of bounds.
- Text rotation {$angleInDegrees} should be a value between -9
- Cannot get shared component for a pseudo-border.
- Can only get pseudo-border for supervisor.
AI-assisted analysis of PHPOffice/PhpSpreadsheet@65b080eef4 (2026-08-17).
Data as JSON: /api/errors/e68b72a48c2cdabc.
Report an issue: GitHub.