October 27, 2024
Chicago 12, Melborne City, USA
PHP

Issues with Drawing and Formula in PHPSpreadsheet When Modifying XLSX Template


I am working with a pre-existing XLSX workbook template that contains multiple sheets and defined charts. When I read the template and attempt to write data back into it, I encounter several issues, specifically:

Drawing Issues: I receive errors related to sheet15.xml, which is affecting the rendering of charts and shapes.
Formula Issues: I also experience problems with formulas in sheet13.xml, causing them to not calculate correctly or produce errors.
I would appreciate any guidance on how to resolve these issues. Are there specific methods or best practices in PHPSpreadsheet that I should follow to ensure the integrity of the drawings and formulas when modifying the template?

Thank you!

first read the template, then perform some operations, and finally write the modified data back to the template.

require 'vendor/autoload.php'; // Include Composer's autoload file

use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Style\Border;
use PhpOffice\PhpSpreadsheet\Style\Fill;
use PhpOffice\PhpSpreadsheet\Style\Color;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use PhpOffice\PhpSpreadsheet\Worksheet\PageSetup;
use PhpOffice\PhpSpreadsheet\RichText\RichText;
use PhpOffice\PhpSpreadsheet\Chart\Chart;
use PhpOffice\PhpSpreadsheet\Chart\DataSeries;
use PhpOffice\PhpSpreadsheet\Chart\DataSeriesValues;
use PhpOffice\PhpSpreadsheet\Chart\PlotArea;
use PhpOffice\PhpSpreadsheet\Chart\Legend as ChartLegend;
use PhpOffice\PhpSpreadsheet\Chart\Title;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Worksheet\MemoryDrawing;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
$FileName="Template/Management_account_v9.xlsx";
$inputFileType="Xlsx";
        $reader = \PhpOffice\PhpSpreadsheet\IOFactory::createReader($inputFileType);
        $reader->setIncludeCharts(true);
        $objPHPExcel = $reader->load($FileName);
        $calculationEngine = Calculation::getInstance($objPHPExcel);
        $calculationEngine->flushInstance();    

// some perations here code

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=utf-8');
            header('Content-Disposition: attachment;filename="Management Report'.substr($budget_year,2).str_pad($month_expense, 2, "0", STR_PAD_LEFT).'.xlsx"');
            //header('Content-Disposition: attachment;filename="Management Report.xlsx"');
            header('Cache-Control: max-age=0');
            include $this->config->item('include_path').'controllers/clean_buffer.php';
            $objWriter = IOFactory::createWriter($objPHPExcel, 'Xlsx');
            $objWriter->setIncludeCharts(TRUE);
            
            $objWriter->save('php://output');
            $objPHPExcel->disconnectWorksheets(); 
            unset($objPHPExcel);
            exit;



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video