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?

<?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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!