Vie*_*nic 5 php excel phpoffice phpoffice-phpspreadsheet
我有以下基本 PHP 项目(只有一个文件加上 Composer 配置):
作曲家.json
{
"config": {
"optimize-autoloader": true,
"platform": {
"php": "7.4.9"
}
},
"require": {
"phpoffice/phpspreadsheet": "1.10.0"
}
}
Run Code Online (Sandbox Code Playgroud)
索引.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", null, 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');
?>
Run Code Online (Sandbox Code Playgroud)
设置:
$ composer i
Run Code Online (Sandbox Code Playgroud)
当我访问该网址时:
http://localhost/index.php
下载以下 Excel 文件:
其中您有 2 张表:{ Graph, Data }。该图表是根据工作表上的数据进行分类的:数据。
到目前为止,一切都很好。
我的问题是:当我升级时:
"phpoffice/phpspreadsheet": "1.10.0"->"phpoffice/phpspreadsheet": "1.10.1"
(只是补丁更新)
并再次点击相同的 url,尝试打开生成的 Excel 文件时出现以下错误:
我们发现“电子表格 (1).xlsx”中的某些内容存在问题。您希望我们尽力恢复吗?如果您信任此工作簿的来源,请单击“是”。
然后是另一个错误:
并且图表未显示。
知道我需要对上面的代码进行什么修改才能消除这些错误并渲染图表吗?
谢谢!
我找到了解决方案。
在上面的代码中,只需替换:0, // displayBlanksAs-> 'gap', // displayBlanksAs。
在最新版本中,例如:"phpoffice/phpspreadsheet": "1.16",为此定义了一个常量:DataSeries::EMPTY_AS_GAP, // displayBlanksAs。
该常量在早期版本中不存在,例如:1.10.1。
谢谢!