PhpSeadsheet图表未保存和/或损坏文件



所以我正在开发一个报告应用程序,该应用程序需要生成带有图表的Xls文件。不过,图表似乎不起作用。我试着用PhpSpreadsheet github中的示例替换下面的代码,但这也不起作用。

我有以下方法将数据(和图表)插入工作表:

$this->spreadsheet->createSheet();
$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->setTitle($title);
$periodStartArray = array(array('', $periodStartDate, $periodEndDate));
while (!empty($stack)) {
$node = array_pop($stack);
if ($node->getData()['portfolioData'][0] != null) {
if ($node->getData()['portfolioData'][0]->getIncludeInStatusSide()) {
$startValue = (float)$this->getDataPoint($node, 'general', $this->periodStart, 'value');
$startValuePercent = number_format(($startValue / $this->startingTotalValues) * 100, $this->percentDecimals, $decimalPoints, $separators);
$endValue = (float)$this->getDataPoint($node, 'general', $this->periodEnd, 'value');
$endValuePercent = number_format(($endValue / $this->endingTotalValues) * 100, $this->percentDecimals, $decimalPoints, $separators);
$startArray = array($node->getData()['portfolioData'][0]->getNavn(), $startValuePercent, $endValuePercent);
$periodStartArray[] = $startArray;
}
}
}
$maxSize = count($periodStartArray);
$worksheet->fromArray($periodStartArray);
//$title . '!$B$1'
$dataSeriesLabels1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $title . '!$B$1', null, 1),
];
// $maxSize - 1 should give the actual number of data points (since the first entry in the array is the 
// date row
$dataString = "'" . $title . "'" . '!$A$2:$A$' . $maxSize;
$xAxisTickValues1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $dataString, null, $maxSize - 1)
];
$dataString = "'" . $title . "'" . '!$B$2:$B$' . $maxSize;
$dataSeriesValues1 = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $dataString, null, $maxSize - 1)
];
$series = new DataSeries(
DataSeries::TYPE_PIECHART,
null,
range(0, count($dataSeriesValues1) - 1),
$dataSeriesLabels1,
$xAxisTickValues1,
$dataSeriesValues1
);
$layout = new Layout();
$layout->setShowVal(true);
$layout->setShowPercent(false);
$plotArea = new PlotArea($layout, array($series));
$legend = new Legend(Legend::POSITION_RIGHT, null, false);
$title = new Title($title);
$chart1 = new Chart(
'chart1',
$title,
$legend,
$plotArea,
true, 
0, 
null, 
null
);
$chart1->setTopLeftPosition('A10');
$chart1->setBottomRightPosition('P25');
$worksheet->addChart($chart1);

它成功地将数据插入电子表格,但不打印任何图表。这是我保存电子表格的地方:

$writer = new PhpOfficePhpSpreadsheetWriterXls($this->spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($this->outputFile); 

所以我包括了图表,但没有图表出现。当我将格式更改为Xlsx时,它似乎至少试图插入图表,但它们已损坏,Excel将删除它们。这只是简单的数据。实际上,a列中有一行公司名称,B列和C列中有两个日期值。当我在excel文件生成后手动插入图表时,它没有问题。

使用此

DataSeries::EMPTY_AS_GAP, // displayBlanksAs

为0

类似于样本文件

https://github.com/PHPOffice/PhpSpreadsheet/blob/master/samples/templates/chartSpreadsheet.php

创建图表时,将0更改为'gap'。

$chart1 = new Chart(
'chart1',
$title,
$legend,
$plotArea,
true, 
'gap', // use 'gap' instead of 0, 
null, 
null

);

以下是PhPS电子表格的问题

Excel 2003/2013/2019 无法打开包含图表的文件

最新更新