当我尝试添加图形时,PhpSpreadsheet 致命错误

问题描述

致命错误:未捕获的 PHPOffice\PHPSpreadsheet\Calculation\Exception: 公式错误:发生意外错误 C:\xampp\htdocs\graficas_excel\vendor\PHPoffice\PHPspreadsheet\src\PHPSpreadsheet\Calculation\Calculation.PHP:5224
堆栈跟踪:
#0 C:\xampp\htdocs\graficas_excel\vendor\PHPoffice\PHPspreadsheet\src\PHPSpreadsheet\Calculation\Calculation.PHP(4305): PHPOffice\PHPSpreadsheet\Calculation\Calculation->raiseFormulaError('Formula 错误:...')
#1 C:\xampp\htdocs\graficas_excel\vendor\PHPoffice\PHPspreadsheet\src\PHPSpreadsheet\Calculation\Calculation.PHP(3539): PHPOffice\PHPSpreadsheet\Calculation\Calculation->internalParseFormula('! Worksheet $ B...',Object(PHPOffice\PHPSpreadsheet\Cell\Cell))
#2 C:\xampp\htdocs\graficas_excel\vendor\PHPoffice\PHPspreadsheet\src\PHPSpreadsheet\Chart\DataSeriesValues.PHP(363): PHPOffice\PHPSpreadsheet\Calculation\Calculation->_calculateFormulaValue('! 工作表 $ B...',NULL,Object(PHPOffice\PHPSpreadsheet\Cell\Cell))
#3 C:\xampp\htdocs\graficas_excel\vendor\PHPoffice\PHPspreadsh in C:\xampp\htdocs\graficas_excel\vendor\PHPoffice\PHPspreadsheet\src\PHPSpreadsheet\Calculation\Calculation.PHP 在线5224

大家好。

我尝试在 PHPoffice/PHPspreadsheet 的 excel 中创建图表,但是当我尝试添加图表时,应用程序坏了。

我的代码

<?PHP

require '../vendor/autoload.PHP';

use PHPOffice\PHPSpreadsheet\Spreadsheet;
use PHPOffice\PHPSpreadsheet\Writer\Xlsx;
use PHPOffice\PHPSpreadsheet\Chart\Chart;
use PHPOffice\PHPSpreadsheet\Chart\DataSeries;
use PHPOffice\PHPSpreadsheet\Chart\DataSeriesValues;
use PHPOffice\PHPSpreadsheet\Chart\Layout;
use PHPOffice\PHPSpreadsheet\Chart\Legend;
use PHPOffice\PHPSpreadsheet\Chart\PlotArea;
use PHPOffice\PHPSpreadsheet\Chart\Title;
use PHPOffice\PHPSpreadsheet\Chart\Axis;

$spreadsheet = new Spreadsheet();

$worksheet = $spreadsheet->getActiveSheet();

$worksheet->fromArray(
        [
            ['Shinjuku',778,618],['Ikebukuro',566,516],[' Tokyo ',452,549],[' Shinagawa ',378,566],[' Shibuya ',370,669],]
);
$xAxisTickValues = [
    New DataSeriesValues(DataSeriesValues :: DATASERIES_TYPE_STRING,'! Worksheet $ A $ 1: $ A $ 5',Null,5),//Shinjuku-Shibuya
];

$dataSeriesValues = [
    New DataSeriesValues(DataSeriesValues :: DATASERIES_TYPE_NUMBER,'! Worksheet $ B $ 1: $ B $ 5',//each value of the number of passengers
];

$series = new DataSeries(
        DataSeries::TYPE_BARCHART,//plottype
        DataSeries::GROUPING_STANDARD,//plotGrouping
        range(0,count($dataSeriesValues) - 1),//plotOrder
        [],//plotLabel
        $xAxisTickValues,//plotCategory
        $dataSeriesValues//plotValues
);

$series->setPlotDirection(DataSeries::DIRECTION_COL);
$plotArea = new PlotArea(null,[$series]);
$title = new Title('number of passengers per day');

$yaxis = new Axis();
$xaxis = new Axis();
$yaxis->setAxisOptionsproperties('low',null,100,null);
$yaxis->setLineParameters('FFFFFF',Axis::EXCEL_COLOR_TYPE_ARGB);
$xaxis->setAxisOptionsproperties('low',null);
$legend1 = new Legend(Legend::POSITION_RIGHT,false);
$yAxisLabel = new Title('');
$chart = new Chart(
        'bar chart',//name
        $title,//title
        $legend1,//legend
        $plotArea,//plotArea
        true,//plotVisibleOnly
        DataSeries::EMPTY_AS_GAP,// displayBlanksAs
        null,// xAxisLabel
        $yAxisLabel,// yAxisLabel
        $yaxis,$xaxis
);

$worksheet->addChart($chart);

$writer = new Xlsx($spreadsheet);

$writer->setIncludeCharts(true);

$writer->save('passengers.xlsx');

解决方法

我已经更改了我的代码。

$worksheet->fromArray(
        [
            ['','number of passengers per day',''],['Shinjuku',7787.81,618],['Ikebukuro ',566,516],[' Tokyo ',452,549],[' Shinagawa ',378,566],[' Shibuya ',370,669],]
);

$xAxisTickValues = [
    New DataSeriesValues(DataSeriesValues :: DATASERIES_TYPE_STRING,'Worksheet!$A$2:$A$6',Null,5),//Shinjuku-Shibuya
];

$dataSeriesValues = [
    New DataSeriesValues(DataSeriesValues :: DATASERIES_TYPE_NUMBER,'Worksheet!$B$2:$B$6',//each value of the number of passengers
    New DataSeriesValues(DataSeriesValues :: DATASERIES_TYPE_NUMBER,'Worksheet!$C$2:$C$6',//each value of the number of passengers
];

它可以工作,但是当我打开 excel 时,我需要恢复表单 xml 错误?