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
Add Header Logo In Excel Files In PHP Using PHPSpreadSheet
May 3, 2023
Add Cell Borders
May 1, 2023
Unmerge Cells
May 1, 2023
Count Google SpreadSheets Cells With Text In PHP Using Google Sheets API PHP Client
April 29, 2023
Create Excel Files With Max Formula In PHP Using PHPSpreadSheet
April 29, 2023
Create Excel Files With Average Formula In PHP Using PHPSpreadSheet
April 29, 2023
Create Excel Files With Sum Formula In PHP Using PHPSpreadSheet
April 26, 2023
Duplicate Google Sheets Sheet Using Google Sheets API PHP Client
April 23, 2023
Update Google Sheets Cells Using Google Sheets API PHP Client
April 22, 2023
Browse by technique