【问题标题】:PhpSpreadsheet fatal errro when i tried add graphic当我尝试添加图形时,PhpSpreadsheet 致命错误
【发布时间】:2021-07-18 09:05:31
【问题描述】:

致命错误:未捕获的 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('公式 错误:...')
#1 C:\xampp\htdocs\graficas_excel\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Calculation\Calculation.php(3539): PhpOffice\PhpSpreadsheet\Calculation\Calculation->internalParseFormula('! 工作表 $ 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 在 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', Null, 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, null, null, null, null, 0, 100, null, null);
$yaxis->setLineParameters('FFFFFF', 100, Axis::EXCEL_COLOR_TYPE_ARGB);
$xaxis->setAxisOptionsProperties('low', null, null, null, null, null, 0, 0, null, null);
$legend1 = new Legend(Legend::POSITION_RIGHT, null, 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');

【问题讨论】:

  • 不确定是否会有所不同,但在new Chart,应该交换最后两个变量。 $xaxis$yaxis 之前。

标签: php phpoffice-phpspreadsheet


【解决方案1】:

我已经更改了我的代码。

$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', Null, 5), //each value of the number of passengers
    New DataSeriesValues(DataSeriesValues :: DATASERIES_TYPE_NUMBER, 'Worksheet!$C$2:$C$6', Null, 5), //each value of the number of passengers
];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 2022-06-15
    • 1970-01-01
    • 1970-01-01
    • 2019-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多