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\IOFactory; $pdo = new PDO('mysql:host=localhost;dbname=demo;charset=utf8mb4', 'root', '', [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, ]); $inputFile = 'users.xlsx'; $reader = IOFactory::createReaderForFile($inputFile); $reader->setReadDataOnly(true); $spreadsheet = $reader->load($inputFile); $rows = $spreadsheet->getActiveSheet()->toArray(); $headers = array_shift($rows); $sql = 'INSERT INTO users (id, name, email) VALUES (:id, :name, :email) ON DUPLICATE KEY UPDATE name = VALUES(name), email = VALUES(email)'; $statement = $pdo->prepare($sql); $pdo->beginTransaction(); try { $imported = 0; foreach ($rows as $row) { $record = array_combine($headers, $row); if ($record['id'] === null || $record['name'] === null) { continue; } $statement->execute([ ':id' => (int) $record['id'], ':name' => (string) $record['name'], ':email' => (string) $record['email'], ]); $imported++; } $pdo->commit(); } catch (Throwable $e) { $pdo->rollBack(); throw $e; } echo "Done. Imported {$imported} rows into users.\n";
The full script from Import Excel Files Into MySQL 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
Import Excel Files Into MySQL In PHP Using PHPSpreadSheet
July 19, 2026
Download Excel Files In The Browser In PHP Using PHPSpreadSheet
July 18, 2026
How to Convert an Excel File to JSON Using PhpSpreadsheet
July 17, 2026
Convert HTML Table Into Excel
July 16, 2023
Create a Grocery List Template in Excel
May 14, 2023
Insert Pictures In Excel Files In PHP Using PHPSpreadSheet
May 6, 2023
Add Header Logo In Excel Files In PHP Using PHPSpreadSheet
May 3, 2023
Add Cell Borders
May 1, 2023
Unmerge Cells
May 1, 2023
Browse by technique