You are on page 1of 1

<?

php

use PhpOffice\PhpSpreadsheet\Spreadsheet;

//$spreadsheet = new \PhpOffice\PhpSpreadsheet\Spreadsheet();


//$sheet = $spreadsheet->getActiveSheet();

$sheet->setCellValue('A1', 1);
$sheet->setCellValue('A2', 2);
$sheet->setCellValue('A3', 3);

$area = 'Worksheet!$A$1:$A$3';
$dataSeriesValues = $dataseriesLabels = $xAxisTickValues = [
new \PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues('Number', $area),
];

$series = new \PhpOffice\PhpSpreadsheet\Chart\DataSeries(


\PhpOffice\PhpSpreadsheet\Chart\DataSeries::TYPE_BARCHART,
\PhpOffice\PhpSpreadsheet\Chart\DataSeries::GROUPING_STANDARD,
range(0, count($dataSeriesValues) - 1),
$dataseriesLabels,
$xAxisTickValues,
$dataSeriesValues
);

$plotarea = new \PhpOffice\PhpSpreadsheet\Chart\PlotArea(null, [$series]);


$chart = new \PhpOffice\PhpSpreadsheet\Chart(
'chart1', // name
null, // title
null, // legend
$plotarea, // plotArea
true, // plotVisibleOnly
0, // displayBlanksAs
NULL, // xAxisLabel
NULL // yAxisLabel
);

$chart->setTopLeftPosition('A5');
$chart->setBottomRightPosition('H20');
$sheet->addChart($chart);

// save XLSX
$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($spreadsheet);
$writer->setIncludeCharts(true);
$writer->save('template.xlsx'); // saved with the chart

// re-use template
$spreadsheetTpl = \PhpOffice\PhpSpreadsheet\IOFactory::load('template.xlsx');
$worksheetTpl = $spreadsheetTpl->getActiveSheet();
$worksheetTpl->getCell('A1')->setValue(123);
$writerTpl = \PhpOffice\PhpSpreadsheet\IOFactory::createWriter($spreadsheetTpl,
'Xlsx');
$writerTpl->setIncludeCharts(true); // useless since no charts were loaded
$writerTpl->save('template2.xlsx'); // cell A1 has value 123, but the chart is gone

You might also like