问题描述
我有以下基本的 PHP 项目(只有一个文件加上 Composer 配置):
composer.json
{
"config": {
"optimize-autoloader": true,"platform": {
"PHP": "7.4.9"
}
},"require": {
"PHPoffice/PHPspreadsheet": "1.10.0"
}
}
index.PHP
<?PHP
require_once __DIR__ . '/vendor/autoload.PHP';
function errorHandler() {
return true;
}
set_error_handler('errorHandler');
$sheets = array(
array('index' => 0,'title' => 'Graph'),array('index' => 1,'title' => 'Data'),);
$PHPSpreadsheetobject = new \PHPOffice\PHPSpreadsheet\Spreadsheet();
foreach ($sheets as $sheet) {
$name = $sheet['title'];
if ($sheet['index']) {
$worksheet[$name] = $PHPSpreadsheetobject->createSheet($sheet['index']);
} else {
$worksheet[$name] = $PHPSpreadsheetobject->getActiveSheet();
}
$PHPSpreadsheetobject->setActiveSheetIndex($sheet['index']);
$worksheet[$name]->setTitle($sheet['title']);
}
$sheet = 'Graph'; // !!! SHEET CHANGE
$PHPSpreadsheetobject->setActiveSheetIndex(1);
$worksheet[$sheet]->getColumnDimension('A')->setWidth("50");
// Charts
// Clients Chart
$xAxisTickValues = array(new \PHPOffice\PHPSpreadsheet\Chart\DataSeriesValues('String',"'Data'!A2:A4",null,3));
$dataSeriesValues = array(new \PHPOffice\PHPSpreadsheet\Chart\DataSeriesValues('Number',"'Data'!B2:B4",3));
$chartSeries = new \PHPOffice\PHPSpreadsheet\Chart\DataSeries(
\PHPOffice\PHPSpreadsheet\Chart\DataSeries::TYPE_BARCHART,// plottype
\PHPOffice\PHPSpreadsheet\Chart\DataSeries::GROUPING_CLUSTERED,// plotGrouping
range(0,count($dataSeriesValues) - 1),// plotOrder
[],// plotLabel
$xAxisTickValues,// plotCategory
$dataSeriesValues // plotValues
);
$chartSeries->setPlotDirection(\PHPOffice\PHPSpreadsheet\Chart\DataSeries::DIRECTION_COLUMN);
$plotArea = new \PHPOffice\PHPSpreadsheet\Chart\PlotArea(null,array($chartSeries));
$title = new \PHPOffice\PHPSpreadsheet\Chart\Title('Clients');
$yAxisLabel = new \PHPOffice\PHPSpreadsheet\Chart\Title('');
$charts = new \PHPOffice\PHPSpreadsheet\Chart\Chart(
'clients',// name
$title,// title
null,// legend
$plotArea,// plotArea
true,// plotVisibleOnly
0,// displayBlanksAs
null,// xAxisLabel
$yAxisLabel // yAxisLabel
);
$charts->setTopLeftPosition('A1');
$charts->setBottomrightPosition('B19');
$worksheet[$sheet]->addChart($charts);
$sheet = 'Data'; // !!! SHEET CHANGE
$PHPSpreadsheetobject->setActiveSheetIndex(1);
$dataArray = array(
1 => array('Date','Clients'),2 => array(date('m/d/y',strtotime('01/01/2021')),'500'),3 => array(date('m/d/y',strtotime('01/02/2021')),'725'),4 => array(date('m/d/y',strtotime('01/03/2021')),'930'),);
foreach (range('A','B') as $columnID) {
$worksheet[$sheet]->getColumnDimension($columnID)->setAutoSize(true);
}
$worksheet[$sheet]->fromArray($dataArray,' ','A1');
// set the first tab as active
$PHPSpreadsheetobject->setActiveSheetIndex(0);
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header("Content-disposition: attachment;filename=Spreadsheet.xlsx");
header('Cache-Control: max-age=0');
$objWriter = new \PHPOffice\PHPSpreadsheet\Writer\Xlsx($PHPSpreadsheetobject);
$objWriter->setIncludeCharts(true);
$objWriter->save('PHP://output');
?>
设置:
$ composer i
当我访问网址时:
http://localhost/index.PHP
下载以下 Excel 文件:
您有 2 张纸:{ Graph,Data }。图表根据表格中的数据分类:数据。
到目前为止一切顺利。
我的问题是:当我升级时:
"PHPoffice/PHPspreadsheet": "1.10.0"
-> "PHPoffice/PHPspreadsheet": "1.10.1"
(只是补丁更新)
再次点击相同的网址,尝试打开生成的 Excel 文件时出现以下错误:
我们发现“电子表格 (1).xlsx”中的某些内容存在问题。你想让我们尽量恢复吗?如果您信任此工作簿的来源,请单击“是”。
并且没有显示图表。
知道我需要对上面的代码做哪些修改才能消除这些错误并呈现图形吗?
谢谢!
解决方法
我找到了解决方案。
在上面的代码中,只需替换:0,// displayBlanksAs
-> 'gap',// displayBlanksAs
。
在最新版本等:"phpoffice/phpspreadsheet": "1.16"
,存在一种用于此定义的常数:DataSeries::EMPTY_AS_GAP,// displayBlanksAs
这常数不存在于早期版本如下:1.10.1
谢谢!