public function writeSpreadsheet(PhpPresentation $presentation, $chart, $tempName)
{
// Need output?
if (!$chart->hasIncludedSpreadsheet()) {
throw new \Exception('No spreadsheet output is required for the given chart.');
}
// Verify PHPExcel
if (!class_exists('PHPExcel')) {
throw new \Exception('PHPExcel has not been loaded. Include PHPExcel.php in your script, e.g. require_once \'PHPExcel.php\'.');
}
// Create new spreadsheet
$workbook = new \PHPExcel();
// Set properties
$title = $chart->getTitle()->getText();
if (strlen($title) == 0) {
$title = 'Chart';
}
$workbook->getProperties()->setCreator($presentation->getDocumentProperties()->getCreator())->setLastModifiedBy($presentation->getDocumentProperties()->getLastModifiedBy())->setTitle($title);
// Add chart data
$sheet = $workbook->setActiveSheetIndex(0);
$sheet->setTitle('Sheet1');
// Write series
$seriesIndex = 0;
foreach ($chart->getPlotArea()->getType()->getSeries() as $series) {
// Title
$sheet->setCellValueByColumnAndRow(1 + $seriesIndex, 1, $series->getTitle());
// X-axis
$axisXData = array_keys($series->getValues());
$numAxisXData = count($axisXData);
for ($i = 0; $i < $numAxisXData; $i++) {
$sheet->setCellValueByColumnAndRow(0, $i + 2, $axisXData[$i]);
}
// Y-axis
$axisYData = array_values($series->getValues());
$numAxisYData = count($axisYData);
for ($i = 0; $i < $numAxisYData; $i++) {
$sheet->setCellValueByColumnAndRow(1 + $seriesIndex, $i + 2, $axisYData[$i]);
}
++$seriesIndex;
}
// Save to string
$writer = \PHPExcel_IOFactory::createWriter($workbook, 'Excel2007');
$writer->save($tempName);
// Load file in memory
$returnValue = file_get_contents($tempName);
if (@unlink($tempName) === false) {
throw new \Exception('The file ' . $tempName . ' could not removed.');
}
return $returnValue;
}