php - 我们发现“Spreadsheet.xlsx”中的某些内容存在问题。你想让我们尽可能多地恢复吗?
问题描述
我有以下基本 PHP 项目(只有一个文件加上 composer 配置):
作曲家.json
{
"config": {
"optimize-autoloader": true,
"platform": {
"php": "7.4.9"
}
},
"require": {
"phpoffice/phpspreadsheet": "1.10.0"
}
}
索引.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');
?>
设置:
$ 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
.
谢谢!
推荐阅读
- c# - Html.BeginForm 后空模型
- amazon-dynamodb - 如何避免热分区并仍然提供必要的访问模式
- excel - 从 Outlook 运行 Excel 宏
- yii2 - 选定的 select2 小部件将更新其他 select2
- node.js - 控制器文件在 Nginx 上更新但服务器仍然提供旧内容
- r - cumsum NAs 和其他条件 R
- c++ - 使用 vector.sort() 无效使用非静态成员函数
- java - Apache POI - java.lang.NoClassDefFoundError: org/apache/commons/compress/utils/InputStreamStatistics
- angular - 使用 ANGULAR 8 从 api 下载文件
- sql - 如何以基于集合的方式解析逗号分隔值?