PHPExcel - formatting is lost when editing code

These days I play with PHP integration - MS Excel. My job is to open an existing table, add some data and save the filled table as a new file. Basically, this is a way to populate a template, eventhough xlsx are used as templates.

I looked at PHPExcel , which looks pretty good. In order to implement the proof of concept, I did the following (minimized to illustrate what I need to do):

$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);  
$objPHPExcel = $objReader->load("myTemplateToFill.xlsx");

    //Here comes the actual filling
$objWorksheet = $objPHPExcel->createSheet();
$objWorksheet->setTitle('Apple')    ;
$objWorksheet->setCellValue('A1', 'Banana');    
$objPHPExcel->setActiveSheetIndex(0);

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
$objWriter->save('myFilledTemplate.xlsx');

Running this, I found that a new file is being created, my daat is inserted, but, unfortunately, all existing formatting is lost.

, , PHPExcel formattigs ? : .. , ?

!

+4
2

Hum, , . , ?

$objPHPExcel->setActiveSheetIndex(0); // index of sheet
$workSheet = $objPHPExcel->getActiveSheet();
$workSheet->setTitle('Pflaume');
+3

- $ ObjReader- > setReadDataOnly (); , .

. . , copyExpression_WorkSheet PHP()

+7

Source: https://habr.com/ru/post/1782431/


All Articles