问题
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?
<?php
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());
$excelReader->setIncludeCharts(true);
$excel = $excelReader->load($excelFIleInfo->getRealPath());
//end loading
$excel->setActiveSheetIndex((int)0);
//add image
$drawing = new Drawing();
$drawing->setPath(__DIR__.'/assets/image.jpg');
$drawing->setCoordinates('Q5');
$drawing->setHeight(80);
$drawing->setWidth(140);
$drawing->setWorksheet($excel->getActiveSheet());
//end add image
//save edited excel to a new file
$writer = new Xlsx($excel);
$writer->setPreCalculateFormulas(false);
$writer->setIncludeCharts(true);
$writer->save($outputExcelFile);
Link to an example of the excel file
回答1:
Php spreadsheet fills it known formatting in a copy/template. All Fromatting that is not known will delete at the save of the file.
来源:https://stackoverflow.com/questions/63790400/phpspreadsheet-loses-styles-of-existing-charts-during-excel-file-editing