我正在将现有的工作程序从以下方面进行迁移。PHPExcel
(phpoffice/phpexcel: 1.8.0
)至 PHPSpreadsheet
在生成包含以下内容的Excel文件时,我遇到了一个问题。一张或多张图表.
没有任何 addChart
调用,生成的文件是有效的,但只要我添加一个图表,生成的文件就变得无效。Excel确实尝试恢复数据,成功了,但没有图表。
Excel恢复工具显示。
Excel completed file level validation and repair.
Some parts of this workbook may have been repaired or discarded.
Removed Part: /xl/drawings/drawing1.xml part. (Drawing shape)
当前使用: PHP 7.1和 phpoffice/phpspreadsheet: "1.10.1"
我的代码与我试图添加的PieChart的示例非常相似。
生成PieChart的部分代码。
$ageCounterRowIndex
有一个值是6$expectedPointCount
价值4D3:D6
E3:E6
$dataSeriesLabels = [];
$xAxisTickValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, "$quotedSheetTitle\$D\$3:\$D\$$ageCounterRowIndex", $expectedPointCount)
];
$dataSeriesValues = [
new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, "$quotedSheetTitle\$E\$3:\$E\$$ageCounterRowIndex", $expectedPointCount),
];
$dataSeries = new DataSeries(
DataSeries::TYPE_PIECHART,
NULL,
range(0, count($dataSeriesValues) - 1),
$dataSeriesLabels,
$xAxisTickValues,
$dataSeriesValues
);
$pieLayout = new Layout();
$pieLayout->setShowVal(TRUE)->setShowPercent(TRUE);
$plotArea = new PlotArea($pieLayout, [ $dataSeries ]);
$legend = new Legend(Legend::POSITION_RIGHT, NULL, FALSE);
$chart = new Chart(
'piechart_' . ($sheetIndex - 1),
null,
$legend,
$plotArea,
true,
0,
NULL,
NULL
);
$chart->setTopLeftPosition('G2');
$chart->setBottomRightPosition('L15');
$sheet->addChart($chart);
更改 displayBlanksAs
的新建图表调用中的参数。0
到 'gap'
为我解决了这个问题。
$chart = new Chart(
'piechart_' . ($sheetIndex - 1),
null,
$legend,
$plotArea,
true,
0,
NULL,
NULL
);
成为
$chart = new Chart(
'piechart_' . ($sheetIndex - 1),
null,
$legend,
$plotArea,
true,
'gap',
NULL,
NULL
);