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\Cell\DataType; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; use PhpOffice\PhpSpreadsheet\Writer\Csv; $rows = [ ['Ada Lovelace', 'Great service'], ['Grace Hopper', '=2+5'], ['Evil User', '=HYPERLINK("http://attacker.example","Refund")'], ['Bjarne S.', '-7 (owed a credit)'], ]; function looksLikeFormula(string $value): bool { return $value !== '' && in_array($value[0], ['=', '+', '-', '@', "\t", "\r"], true); } // 1. Naive export: fromArray auto-detects "=..." as a formula. $bad = new Spreadsheet(); $badSheet = $bad->getActiveSheet(); $badSheet->fromArray($rows, null, 'A1'); (new Xlsx($bad))->save('export-unsafe.xlsx'); echo "Naive export — what the cell actually became:\n"; echo " B2 value : " . $badSheet->getCell('B2')->getValue() . "\n"; echo " B2 calculated : " . $badSheet->getCell('B2')->getCalculatedValue() . "\n"; echo " (the '=2+5' the user typed is now a LIVE formula that evaluates to 7)\n\n"; // 2. XLSX fix: force untrusted cells to explicit strings. $safe = new Spreadsheet(); $safeSheet = $safe->getActiveSheet(); $r = 1; foreach ($rows as $line) { $col = 'A'; foreach ($line as $value) { $safeSheet->setCellValueExplicit($col . $r, (string) $value, DataType::TYPE_STRING); $col++; } $r++; } (new Xlsx($safe))->save('export-safe.xlsx'); echo "Safe XLSX export — same input, forced to text:\n"; echo " B2 value : " . $safeSheet->getCell('B2')->getValue() . "\n"; echo " B2 calculated : " . $safeSheet->getCell('B2')->getCalculatedValue() . "\n"; echo " (stored as text, so it is inert and displays exactly as typed)\n\n"; // 3. CSV has no types: neutralise the leading character with an apostrophe. $csv = new Spreadsheet(); $csvSheet = $csv->getActiveSheet(); $r = 1; foreach ($rows as $line) { $col = 'A'; foreach ($line as $value) { $value = (string) $value; if (looksLikeFormula($value)) { $value = "'" . $value; } $csvSheet->setCellValueExplicit($col . $r, $value, DataType::TYPE_STRING); $col++; } $r++; } (new Csv($csv))->save('export-safe.csv'); echo "Safe CSV export (export-safe.csv):\n"; echo rtrim(file_get_contents('export-safe.csv')) . "\n";
The full script from Prevent Formula Injection In Excel Exports 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
Prevent Formula Injection In Excel Exports In PHP Using PHPSpreadSheet
August 7, 2026
Create An Excel Table In PHP Using PHPSpreadSheet
August 6, 2026
Use VLOOKUP In Excel Files In PHP Using PHPSpreadSheet
August 6, 2026
Read Excel Dates Back Into PHP Using PHPSpreadSheet
August 6, 2026
Convert An Excel File To XML In PHP Using PHPSpreadSheet
August 5, 2026
Save One Spreadsheet As XLSX, XLS And ODS In PHP Using PHPSpreadSheet
August 4, 2026
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
Browse by technique