gpt4 book ai didi

excel - PHPSpreadsheet 生成带有图表的无效文件

转载 作者:行者123 更新时间:2023-12-04 13:16:44 29 4
gpt4 key购买 nike

我正在将现有/正在运行的应用程序从 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 示例非常相似。

生成饼图的部分代码:

  • $ageCounterRowIndex 的值为 6
  • $expectedPointCount 的值为 4
  • 我的数据刻度值位于 D3: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' 为我解决了这个问题。

我在 PhpSpreadsheet repository 上找到了关于它的错误报告

$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
);

关于excel - PHPSpreadsheet 生成带有图表的无效文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59694440/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com