php - 我们发现 'Spreadsheet.xlsx' 中的某些内容存在问题。你想让我们尽可能多地恢复吗?

标签 php excel phpoffice phpoffice-phpspreadsheet

我有以下基本 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", 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 文件:
enter image description here
你有 2 张纸:{ Graph, Data }。图表是根据工作表上的数据进行分类的:数据。
到目前为止,一切都很好。
我的问题是:当我升级时:"phpoffice/phpspreadsheet": "1.10.0" -> "phpoffice/phpspreadsheet": "1.10.1"(只是补丁更新)
并再次点击相同的网址,尝试打开生成的 Excel 文件时出现以下错误:
enter image description here

We found a problem with some content in 'Spreadsheet (1).xlsx'. Do you want us to try to recover as much as we can? if you trust the source of this workbook, click Yes.


然后是另一个错误:
enter image description here
并且图表没有显示。
知道我需要对上面的代码进行哪些修改以消除这些错误并渲染图形吗?
谢谢!

最佳答案

我找到了解决方案。
在上面的代码中,只需替换:0, // displayBlanksAs -> 'gap', // displayBlanksAs .
在最新版本上,例如:"phpoffice/phpspreadsheet": "1.16" ,为此定义了一个常量:DataSeries::EMPTY_AS_GAP, // displayBlanksAs .
该常量在早期版本中不存在,例如:1.10.1 .
谢谢!

关于php - 我们发现 'Spreadsheet.xlsx' 中的某些内容存在问题。你想让我们尽可能多地恢复吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65609921/

相关文章:

php - 带有 PHP 循环的 CSS 代码结构第 2 轮

php - 优化 MySQL 或 PHP 的搜索功能

Microsoft Excel WorksheetFunction 上的 Python win32com

php - 如何在php phpspreadsheet中使用excel sum公式?

phpoffice/phpspreadsheet - 如何从 getHighestRow() 中排除没有值的单元格

php - Symfony 全新安装无法在我的本地主机上运行

php - 从选择查询设置变量

excel - 使用基于单元格值的excel VBA打开文件,路径由相同的值动态组成

python - 如果某些列有交集,如何将行从一张纸复制到另一张纸

PHPOffice PHPPresentation 在保存 pptx 时丢失原始样式