Spreadsheet class is the core container for workbook data in PhpSpreadsheet. It manages worksheets, document properties, styles, and named ranges.
Class Overview
Namespace:PhpOffice\PhpSpreadsheet
Source: src/PhpSpreadsheet/Spreadsheet.php
The Spreadsheet class represents an entire workbook and provides methods for managing worksheets, document metadata, styles, and calculations.
Constructor
__construct()
Creates a new PhpSpreadsheet with one worksheet.
- Creates one default worksheet
- Initializes document properties
- Sets up document security
- Creates the calculation engine
- Initializes default styles
Worksheet Management
getActiveSheet()
Get the currently active worksheet.
The active worksheet object
createSheet(?int $sheetIndex = null)
Create a new worksheet and add it to the workbook.
Index position where sheet should be inserted (0-based). Use null to add at the end.
The newly created worksheet
addSheet(Worksheet $worksheet, ?int $sheetIndex = null, bool $retitleIfNeeded = false)
Add an existing worksheet to the workbook.
The worksheet object to add
Index position (0-based), or null to add at the end
Automatically rename if a sheet with the same name exists
The added worksheet
getSheet(int $sheetIndex)
Get a worksheet by its index position.
Zero-based index of the worksheet
The requested worksheet
getSheetByName(string $worksheetName)
Get a worksheet by its name.
Name of the worksheet to retrieve
The worksheet, or null if not found
getSheetByNameOrThrow(string $worksheetName)
Get a worksheet by name, throwing an exception if not found.
Name of the worksheet
The worksheet
getAllSheets()
Get all worksheets in the workbook.
Array of all worksheets
getSheetCount()
Get the total number of worksheets.
Number of worksheets in the workbook
removeSheetByIndex(int $sheetIndex)
Remove a worksheet by its index position.
Zero-based index of the worksheet to remove
setActiveSheetIndex(int $worksheetIndex)
Set the active sheet by index.
Zero-based index of the worksheet to activate
The newly activated worksheet
Document Properties
getProperties()
Get document properties (metadata).
Document properties object
setProperties(Properties $documentProperties)
Set document properties.
The properties object to set
Named Ranges
addNamedRange(NamedRange $namedRange)
Add a named range to the workbook.
The named range object to add
getNamedRange(string $namedRange, ?Worksheet $worksheet = null)
Get a named range.
Name of the range
Scope worksheet (null for global scope)
The named range, or null if not found
getNamedRanges()
Get all named ranges.
Array of all named ranges
removeNamedRange(string $namedRange, ?Worksheet $worksheet = null)
Remove a named range.
Name of the range to remove
Scope worksheet (null for global scope)
Styling
getDefaultStyle()
Get the default style for the workbook.
The default style object
addCellXf(Style $style)
Add a cell style format (XF) to the workbook.
The style to add
Calculation
getCalculationEngine()
Get the calculation engine.
The calculation engine instance

