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\Writer\Xlsx; use \PhpOffice\PhpSpreadsheet\Chart\Chart; use \PhpOffice\PhpSpreadsheet\Chart\DataSeries; use \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues; use \PhpOffice\PhpSpreadsheet\Chart\Legend; use \PhpOffice\PhpSpreadsheet\Chart\PlotArea; use \PhpOffice\PhpSpreadsheet\Chart\Title; $spreadsheet = new Spreadsheet(); $worksheet = $spreadsheet->getActiveSheet(); $worksheet->setTitle('Sales'); // The data the chart plots. $worksheet->fromArray([ ['Month', 'Sales'], ['Jan', 120], ['Feb', 145], ['Mar', 98], ['Apr', 172], ]); // The category axis — the Month labels. $categories = [ new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Sales!$A$2:$A$5', null, 4), ]; // The values to plot — the Sales numbers. $values = [ new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, 'Sales!$B$2:$B$5', null, 4), ]; // The series name — the "Sales" header in B1. $labels = [ new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, 'Sales!$B$1', null, 1), ]; // A bar chart, drawn as vertical columns. $series = new DataSeries( DataSeries::TYPE_BARCHART, DataSeries::GROUPING_CLUSTERED, range(0, count($values) - 1), $labels, $categories, $values ); $series->setPlotDirection(DataSeries::DIRECTION_COL); $plotArea = new PlotArea(null, [$series]); $legend = new Legend(Legend::POSITION_RIGHT, null, false); $title = new Title('Monthly Sales'); $chart = new Chart('sales-chart', $title, $legend, $plotArea); // Anchor the chart onto the worksheet. $chart->setTopLeftPosition('D2'); $chart->setBottomRightPosition('J17'); $worksheet->addChart($chart); // The writer only emits charts when told to. $writer = new Xlsx($spreadsheet); $writer->setIncludeCharts(true); $writer->save('chart.xlsx'); echo "Done. Wrote chart.xlsx with a column chart.\n";
The full script from Create An Excel Chart 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
Create An Excel Chart In PHP Using PHPSpreadSheet
July 25, 2026
Work With Multiple Worksheets In Excel Files In PHP Using PHPSpreadSheet
July 24, 2026
Convert A CSV File To Excel In PHP Using PHPSpreadSheet
July 23, 2026
Read Large Excel Files In Chunks In PHP Using PHPSpreadSheet
July 22, 2026
Read Formula Results In Excel Files In PHP Using PHPSpreadSheet
July 21, 2026
Export MySQL Data To Excel Files In PHP Using PHPSpreadSheet
July 20, 2026
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
Browse by technique