The most common format, using column letters and row numbers:
// Set cell value using A1 notation$sheet->setCellValue('A1', 'Hello');$sheet->setCellValue('B5', 123);$sheet->setCellValue('AA100', 'Column AA, Row 100');// Get cell value$value = $sheet->getCell('C3')->getValue();
1-Based Indexing: In PhpSpreadsheet, column and row indices are 1-based, not 0-based. This means A1 equals [1, 1], not [0, 0]. This matches Excel’s COLUMN() and ROW() functions.
For more complex scenarios, use CellAddress objects:
use PhpOffice\PhpSpreadsheet\Cell\CellAddress;// Create CellAddress$address = new CellAddress('B3');$sheet->setCellValue($address, 'Value');// With optional worksheet reference$address = new CellAddress('B3', $sheet);
$data = [ [null, 2010, 2011, 2012], ['Q1', 12, 15, 21], ['Q2', 56, 73, 86], ['Q3', 52, 61, 69], ['Q4', 30, 32, 40],];$sheet->fromArray( $data, // The data to set null, // Array values with this value will not be set 'C3' // Top left coordinate where to start);
// Get entire worksheet as array$allData = $sheet->toArray();// Get specific range$rangeData = $sheet->rangeToArray( 'C3:E5', // Range to retrieve null, // Value for empty cells true, // Calculate formulas true, // Format values true // Index by cell row and column);// Get named range$namedData = $sheet->namedRangeToArray('SalesData');
// Get a cell object$cell = $sheet->getCell('A1');// Set value via cell object$cell->setValue('Hello World');// Get value$value = $cell->getValue();// Get calculated value (for formulas)$calculatedValue = $cell->getCalculatedValue();// Get formatted value (with number formatting)$formattedValue = $cell->getFormattedValue();
Detached Cell References: When you assign a cell to a variable (e.g., $cell = $sheet->getCell('A1')), subsequent calls to getCell() for other cells will detach that variable’s link to the cell collection. The cell’s value remains accessible, but methods requiring the collection link (like getCoordinate()) will fail.To avoid this issue, either:
Don’t store cell objects in variables for long periods
Re-fetch the cell when needed
Use cell values directly instead of storing cell objects
use PhpOffice\PhpSpreadsheet\Cell\DataType;// Force string type (preserves leading zeros)$sheet->setCellValueExplicit( 'A1', '01234567890', DataType::TYPE_STRING);// Force numeric$sheet->setCellValueExplicit( 'A2', '123', DataType::TYPE_NUMERIC);
$highestRow = $sheet->getHighestDataRow();$highestColumn = $sheet->getHighestDataColumn();$highestColumn++; // Increment past the last column for loop terminationfor ($row = 1; $row <= $highestRow; $row++) { for ($col = 'A'; $col != $highestColumn; $col++) { $cellAddress = $col . $row; $value = $sheet->getCell($cellAddress)->getValue(); echo "$cellAddress: $value\n"; }}
PHP 8.5+ String Increment Change: In PHP 8.5+, the behavior of ++ on strings changed. Use StringHelper::stringIncrement($col) instead of ++$col for column letter incrementing.
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;// For PHP 8.5+for ($col = 'A'; $col != $highestColumn; $col = StringHelper::stringIncrement($col)) { // ...}
// Merge cells$sheet->mergeCells('A1:C1');// Unmerge cells$sheet->unmergeCells('A1:C1');// Check if cell is in a merged range$isMerged = $sheet->getCell('B1')->isInMergeRange();// Get all merged cell ranges$mergedRanges = $sheet->getMergeCells();
// Get the highest row with data$highestRow = $sheet->getHighestDataRow();// Get the highest column with data$highestColumn = $sheet->getHighestDataColumn();// Get as a coordinate$highestCell = $sheet->getHighestDataColumn() . $sheet->getHighestDataRow();echo $highestCell; // e.g., 'F10'// Get dimension range$dimension = $sheet->calculateWorksheetDimension();echo $dimension; // e.g., 'A1:F10'