PHPSpreadsheet loses styles of existing Charts during excel file editing

二次信任 提交于 2021-01-29 17:41:49


I use PHPSpreadsheet to edit existed Excel file and add there some images. But sometimes it losses the styles of existed charts in an excel. It resets a background color or a scale of the chart. Some charts are left without changes.

Could someone say why PHPSpreadsheet resets styles/scale of existed charts and how to suppress that side-effect?


include 'vendor/autoload.php';

use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;

$inputExcelPath = __DIR__.'/assets/badExcel/an_excel_file.xlsx';
$outputExcelFile = __DIR__.'/excelOutput/'.time().'_'.'/an_excel_file.xlsx';

//load excel
$excelFIleInfo = new SplFileInfo($inputExcelPath);
$excelReader = IOFactory::createReaderForFile($excelFIleInfo->getRealPath());
$excel = $excelReader->load($excelFIleInfo->getRealPath());
//end loading


//add image
$drawing = new Drawing();
//end add image

//save edited excel to a new file
$writer = new Xlsx($excel);

Link to an example of the excel file


Php spreadsheet fills it known formatting in a copy/template. All Fromatting that is not known will delete at the save of the file.