phpSpreadsheet: Excel file won't open if it includes a chart - but it was working for over a year

94 views Asked by At

I have a php script written that creates an xlsx (using phpSpreadsheet) file with multiple tabs and charts on each tab. This worked for over a year flawlessly. All of a sudden, with zero coding changes made, the excel file now states it is corrupted. Upon repair, the file does open with the charts. The error when viewing the Excel repair log reads:

<repairedRecord>
Repaired Records: Drawing from /xl/drawings/drawing1.xml part (Drawing shape)
</repairedRecord>
<repairedRecord>
Repaired Records: Drawing from /xl/drawings/drawing2.xml part (Drawing shape)
</repairedRecord>

I used composer to update phpSpreadsheet to 1.29 (latest version). I had been on 1.23 earlier.

Here are the important elements of the code:

require_once(__DIR__ . '/vendor/autoload.php');
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\Layout;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Spreadsheet;

// Create the chart
$chart1 = new Chart(
    'chart1', // name
    $title1, // title
    $legend1, // legend
    $plotArea1, // plotArea
    true, // plotVisibleOnly
    DataSeries::EMPTY_AS_GAP, // displayBlanksAs
    null, // xAxisLabel
    null   // yAxisLabel - Pie charts don't have a Y-Axis
);

// Set the position where the chart should appear in the worksheet
$chart1->setTopLeftPosition('J5');
$chart1->setBottomRightPosition('l20');

// Add the chart to the worksheet
$worksheet->addChart($chart1);

Any idea what could have changed that stopped this from working? Remember, this was working. No coding changes. File does get created but it's corrupted and it's fixable.

0

There are 0 answers