简体   繁体   中英

PhpSpreadsheet charts not saving and/or corrupting file

So I'm working on a reporting application that requires generating Xls files with charts. The charts don't appear to be working though. I've tried just replacing the code below with the example in the PhpSpreadsheet github, but that also doesn't work.

I have the following for inserting the data (and chart) into the worksheet:

$this->spreadsheet->createSheet();

$worksheet = $this->spreadsheet->getActiveSheet();
$worksheet->setTitle($title);
$periodStartArray = array(array('', $periodStartDate, $periodEndDate));

while (!empty($stack)) {
    $node = array_pop($stack);
    if ($node->getData()['portfolioData'][0] != null) {
        if ($node->getData()['portfolioData'][0]->getIncludeInStatusSide()) {
            $startValue = (float)$this->getDataPoint($node, 'general', $this->periodStart, 'value');
            $startValuePercent = number_format(($startValue / $this->startingTotalValues) * 100, $this->percentDecimals, $decimalPoints, $separators);

            $endValue = (float)$this->getDataPoint($node, 'general', $this->periodEnd, 'value');
            $endValuePercent = number_format(($endValue / $this->endingTotalValues) * 100, $this->percentDecimals, $decimalPoints, $separators);

            $startArray = array($node->getData()['portfolioData'][0]->getNavn(), $startValuePercent, $endValuePercent);

            $periodStartArray[] = $startArray;
        }
    }
}

$maxSize = count($periodStartArray);
$worksheet->fromArray($periodStartArray);
//$title . '!$B$1'

$dataSeriesLabels1 = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $title . '!$B$1', null, 1),
];

// $maxSize - 1 should give the actual number of data points (since the first entry in the array is the 
// date row

$dataString = "'" . $title . "'" . '!$A$2:$A$' . $maxSize;
$xAxisTickValues1 = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_STRING, $dataString, null, $maxSize - 1)
];

$dataString = "'" . $title . "'" . '!$B$2:$B$' . $maxSize;
$dataSeriesValues1 = [
    new DataSeriesValues(DataSeriesValues::DATASERIES_TYPE_NUMBER, $dataString, null, $maxSize - 1)
];

$series = new DataSeries(
    DataSeries::TYPE_PIECHART,
    null,
    range(0, count($dataSeriesValues1) - 1),
    $dataSeriesLabels1,
    $xAxisTickValues1,
    $dataSeriesValues1
);

$layout = new Layout();
$layout->setShowVal(true);
$layout->setShowPercent(false);

$plotArea = new PlotArea($layout, array($series));
$legend = new Legend(Legend::POSITION_RIGHT, null, false);

$title = new Title($title);

$chart1 = new Chart(
    'chart1',
    $title,
    $legend,
    $plotArea,
    true, 
    0, 
    null, 
    null
);

$chart1->setTopLeftPosition('A10');
$chart1->setBottomRightPosition('P25');
$worksheet->addChart($chart1);

It successfully inserts the data into the spreadsheet, but does not print out any charts. Here is where I'm saving the spreadsheet:

$writer = new \PhpOffice\PhpSpreadsheet\Writer\Xls($this->spreadsheet);
$writer->setIncludeCharts(true);
$writer->save($this->outputFile); 

So I'm including the charts, but no charts appear. When I change the format to Xlsx, it seems to at least try to insert the charts but they're corrupt and Excel removes them. It's just simple data. Literally one row of a company name in the A column and two dates of values down the B and C columns. When I manually insert the charts after the excel file gets generated it works no problem.

use this

DataSeries::EMPTY_AS_GAP, // displayBlanksAs

instaea of 0

like in sample file

https://github.com/PHPOffice/PhpSpreadsheet/blob/master/samples/templates/chartSpreadsheet.php

Change the 0 to 'gap' when creating the chart.

$chart1 = new Chart(
'chart1',
$title,
$legend,
$plotArea,
true, 
'gap', // use 'gap' instead of 0, 
null, 
null

);

Below is the issue for PhPSpreadsheet

File containing a chart can not be opened by Excel 2003/2013/2019

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM