phpoffice/phpspreadsheet
PhpSpreadsheet is a pure-PHP library for reading and writing spreadsheet files (Excel, LibreOffice Calc, and more). Create, edit, and export workbooks with rich formatting, formulas, and multiple formats via a clean, well-documented API.
Begin by installing via Composer:
composer require phpoffice/phpspreadsheet
The entry point is the PhpOffice\PhpSpreadsheet\Spreadsheet class. A minimal example:
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 'Hello World');
$writer = new Xlsx($spreadsheet);
$writer->save('hello-world.xlsx');
For quick starts, check the official samples repo — especially 01_Simple.php and 02_Reading_writing_spreadsheet.php.
For ODS (OpenDocument Spreadsheet) support, use IOFactory::load($path) with the .ods extension to read files, now with improved style handling.
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="report.xlsx"');
$writer->save('php://output');
IOFactory::load($path) and iterate over getActiveSheet()->getRowIterator(). For ODS files, styles (fonts, fills, borders) are now preserved more accurately.$checkbox = new \PhpOffice\PhpSpreadsheet\Style\Conditional\Checkbox();
$sheet->getStyle('A1')->applyFromArray($checkbox->getConditionalStyles());
$reader = IOFactory::createReaderForFile('file.xlsx');
$reader->setReadDataOnly(true);
$reader->setWhitelistImages(true); // Allow external images
$spreadsheet = $reader->load('file.xlsx');
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Pdf\Mpdf();
$writer->setHeaderFooter(true);
$writer->setHeader('&L&O Custom Header &P/&N');
$writer->save('output.pdf');
toArray() or getRelatives():
$cell = $sheet->getCell('A1');
$data = $cell->getValue()['value']; // Original value
$data = $cell->getValue()['oldCalculatedValue']; // Old calculated value (if available)
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Html();
$writer->setLineEnding("\n"); // Custom line ending
$writer->setSaveFormulas(true); // Save formulas as data attributes
$writer->save('output.html');
$reader = IOFactory::createReaderForFile('file.ods');
$spreadsheet = $reader->load('file.ods');
// Styles (e.g., fonts, fills) are now preserved more accurately
Ods writer for ODS exports (styles may differ from XLSX).setReadDataOnly(true) or chunked processing. The new value binders in v5.5.0 improve performance when reading/writing cells.Writer/Html::BODY_LINE constant is removed (use setLineEnding() instead).FormulaParser, FormulaToken, insertBitMap, etc.) are deprecated (no replacement needed unless actively using them).$value = \PhpOffice\PhpSpreadsheet\Shared\Date::excelToDateTimeObject($cellValue);
UNION in formulas (e.g., =SUM(UNION(A1:A10,B1:B10))).CONCATENATE behaved unexpectedly with mixed data types.$cell->getValue()['oldCalculatedValue'] ?? null;
$reader->setWhitelistImages(['https://trusted.cdn.com']);
$drawing = new \PhpOffice\PhpSpreadsheet\Worksheet\Drawing();
$drawing->setHeight(10); // Millimeters
$writer->setSaveFormulas(false); // Default (safer)
setReadDataOnly(true) for reads.\PhpOffice\PhpSpreadsheet\Settings::setErrorHandlerCallback(function($error) {
throw new \RuntimeException($error);
});
Writer\AbstractWriter for new formats (e.g., Markdown tables).Style\StyleCollection for consistent branding.How can I help you explore Laravel packages today?