PHPExcel removes chart style when template is load

2019-07-30 14:37发布

问题:

I am using PHPExcel to read and write values to Excel. I have a template that has a graph and a style on it.

Here's the issue: After I appended some value on the Excel sheet, the style of the chart has been remove and it shows the default style.

I am using this code to read:

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
//  Tell the reader to include charts when it loads a file
$objReader->setIncludeCharts(TRUE);
//  Load the file
$objPHPExcel = $objReader->load("rcca.xlsx");

And this to write

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->setIncludeCharts(TRUE);
$objWriter->save('rcca.xlsx');

Am I missing something?

Please see the screenshot of the chart style before and after:

回答1:

It seems that chart styling need to be hard-coded at this time.

On this other thread someone explain how to add some styling PHPExcel graph design (border, graph color, graph inner position)



回答2:

Had the same problems with charts using PHPExcel (my main problem was to change colors of chart bars)

First of all if some one did not know or forgot xlsx files are zip archs

So the main problem is that PHPExcel when saving xlsx file adds default theme file xl/theme/theme1.xml even if you change it at $objReader->load file it will overwrite it to default at $objWriter->save.

Charts colors in that file theme1.xml at <a:accent1> (...2,3,4,5,6) so i took theme1.xml changed colors to which i need, saved and using The ZipArchive class overwrite it after $objWriter->save with simple code

$zip = new ZipArchive;
if ($zip->open('filename.xlsx') === TRUE) {
    $zip->addFile('mytheme1.xml', 'xl/theme/theme1.xml');
    $zip->close();
    echo 'ok';
} else {
    echo 'err';
}

I hope it will help someone