PHPExcel-从代码编辑时格式丢失 [英] PHPExcel - Formatting gets lost when editing from code
问题描述
这些天我在玩PHP-MS Excel集成.我的任务 是打开一个现有的电子表格,添加一些数据并保存填充的 电子表格作为新文件.基本上是模板填充引擎, 即使xlsx文件用作模板.
I'm playing around with an PHP - MS Excel integration these days. My task is to open an existing spreadsheet, add some data and save the filled spreadsheet as a new file. Basically sort of a template filling engine, eventhough xlsx files are used as templates.
我研究了 PHPExcel ,该框架看上去相当不错.为了 为了实施概念验证,我做了以下工作(减少到最低限度) 需要说明我需要做什么):
I looked into PHPExcel which seems to be fairly nice framework. In order to implement a proof of concept I did the following (reduced to the minimum required 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');
运行此命令,我发现创建了一个新文件,插入了我的daat,但可悲的是 所有现有的格式都会丢失.
Running this I found that a new file is created, my daat is inserted but sadly with all of the existing formatting is lost.
所以问题是,有没有办法使PHPExcel在新版本中保留这些formattigs? 文件?或走得更远:是否可以将图表等保存在模板文件中 并按照我尝试的方式填写这些内容?
So the question is, is there a way to make PHPExcel keep those formattigs in the new file? Or to go even further: is it possible to keep charts etc. in a template file and fill those the way I tried?
提前感谢您分享的所有经验!
Thanks in advance for all of your experience shared!
K
推荐答案
哼,您正在创建一个新的workSheet,但默认没有格式.我认为您必须将值添加到现有工作表中吗?
Hum, you are creating an new workSheet, wich has no format as default. I think you have to add your values to the existing sheet ?
$objPHPExcel->setActiveSheetIndex(0); // index of sheet
$workSheet = $objPHPExcel->getActiveSheet();
$workSheet->setTitle('Pflaume');
这篇关于PHPExcel-从代码编辑时格式丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!