PHPSpreadsheet · Google Sheets API · Excel
Spreadsheets, driven by code.
Hands-on PHP tutorials for working with Excel and Google Sheets — read and
write .xlsx, convert files to JSON, stream downloads in the
browser, and insert images, formulas, and styling. Every guide ships with
code you can copy, run, and adapt.
Latest complete code
<?php require 'vendor/autoload.php'; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\NamedRange; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->setTitle('Sales'); $worksheet->fromArray([ ['Item', 'Amount'], ['Keyboard', 40], ['Monitor', 190], ['Mouse', 25], ['Webcam', 55], ], null, 'A1'); $worksheet->setCellValue('D1', 0.20); // Define the names: one range, one single cell. Absolute references. $spreadsheet->addNamedRange(new NamedRange('Amounts', $worksheet, '$B$2:$B$5')); $spreadsheet->addNamedRange(new NamedRange('TaxRate', $worksheet, '$D$1')); // Formulas refer to the names. $worksheet->setCellValue('D3', '=SUM(Amounts)'); $worksheet->setCellValue('D4', '=SUM(Amounts)*TaxRate'); $worksheet->setCellValue('D5', '=SUM(Amounts)*(1+TaxRate)'); (new Xlsx($spreadsheet))->save('named-ranges.xlsx'); echo "Wrote named-ranges.xlsx\n"; echo "Subtotal =SUM(Amounts) : " . $worksheet->getCell('D3')->getCalculatedValue() . "\n"; echo "Tax =SUM(Amounts)*TaxRate : " . $worksheet->getCell('D4')->getCalculatedValue() . "\n"; echo "Total =SUM(Amounts)*(1+TaxRate): " . $worksheet->getCell('D5')->getCalculatedValue() . "\n";
The full script from Use Named Ranges In Excel Files In PHP Using PHPSpreadSheet — copy, run, adapt.
Key methods
IOFactory::load()
PhpSpreadsheet
getActiveSheet()
PhpSpreadsheet
fromArray()
PhpSpreadsheet
getCalculatedValue()
PhpSpreadsheet
save('php://output')
PhpSpreadsheet
spreadsheets_values->get()
Google Sheets
spreadsheets_values->update()
Google Sheets
spreadsheets->create()
Google Sheets
json_encode()
PHP
header()
PHP
Latest articles
Use Named Ranges In Excel Files In PHP Using PHPSpreadSheet
August 3, 2026
Add Rich Text To A Single Cell In Excel Files In PHP Using PHPSpreadSheet
August 2, 2026
List The Worksheets In An Excel File In PHP Using PHPSpreadSheet
August 1, 2026
Convert An Excel File To An HTML Table In PHP Using PHPSpreadSheet
July 31, 2026
Loop Through Cells With Iterators In PHP Using PHPSpreadSheet
July 30, 2026
Convert An Excel File To CSV In PHP Using PHPSpreadSheet
July 29, 2026
Handle An Uploaded Excel File In PHP Using PHPSpreadSheet
July 28, 2026
Convert An Excel File To PDF In PHP Using PHPSpreadSheet
July 27, 2026
Freeze The Header Row In Excel Files In PHP Using PHPSpreadSheet
July 26, 2026
Browse by technique