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; use \PhpOffice\PhpSpreadsheet\Writer\Html; // Load the workbook, keeping styles so the HTML can carry them. $spreadsheet = IOFactory::load('sales.xlsx'); $writer = new Html($spreadsheet); // Write every sheet, not just the active one. $writer->writeAllSheets(); // Put the CSS on each element instead of in a <style> block, so the // table survives being pasted into a page or an email. $writer->setUseInlineCss(true); // A whole HTML document: <html>, <head>, the styles, and the tables. $writer->save('sales.html'); // Just the <table> markup, for dropping into a page you already have. $writer->setSheetIndex(0); $tableOnly = $writer->generateSheetData(); file_put_contents('table-only.html', $tableOnly); echo 'Done. Wrote sales.html (' . strlen(file_get_contents('sales.html')) . " bytes, all sheets)\n"; echo 'and table-only.html (' . strlen($tableOnly) . " bytes, the table markup alone).\n";
The full script from Convert An Excel File To An HTML Table 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
Change A Spreadsheet Title
July 24, 2022
Change Google Spreadsheet Title Using Google Sheets API PHP Client
July 23, 2022
Create A “Hello World” SpreadSheet Using Google Sheets API PHP Client
July 17, 2022
Create A Blank SpreadSheet Using Google Sheets API PHP Client Library
July 13, 2022
Obtain Google API Access Token Through Command Line Using PHP
July 10, 2022
Install The Google Client Library Specifying Google Sheets
July 7, 2022
Enable Google Sheets API
July 4, 2022
Create A Google Credential OAuth Client ID
July 3, 2022
Create A Google OAuth Consent Screen
July 3, 2022
Browse by technique