用PHPExcel编辑excel文件

我正在尝试使用PHPExcel来编辑一个Excel文件,但是我的文件被破坏了。 我的猜测是这是因为文件太复杂了,但我不确定。 一个非常简单的Excel文件没有任何问题。

首先,PHPExcel读取文件,修改数据然后重新创build它? 这不只是打开和写入同一个文件?

我收到这个错误消息:

致命错误:未知的exception'PHPExcel_Exception'消息'无效的单元格坐标(MAX(MATCH(REPLOGRAMS /库/ WebServer / Documents / sb / Classes / PHPExcel / Cell.php(651):PHPExcel_Cell :: coordinateFromString('(MAX(MATCH(REPT …')#1 / Library / WebServer / Documents / sb / Classes / PHPExcel /Cell.php(624):PHPExcel_Cell :: absoluteCoordinate('(MAX(MATCH(REPT …')#2 /Library/WebServer/Documents/sb/Classes/PHPExcel/Writer/Excel2007/Workbook.php(438) :PHPExcel_Cell :: absoluteReference('(MAX(MATCH(REPT …')#3 /Library/WebServer/Documents/sb/Classes/PHPExcel/Writer/Excel2007/Workbook.php(282):PHPExcel_Writer_Excel2007_Workbook-> writeDefinedNameForPrintArea(Object (PHPExcel_Shared_XMLWriter),Object(PHPExcel_Worksheet),0)#4 /Library/WebServer/Documents/sb/Classes/PHPExcel/Writer/Excel2007/Workbook.php(75):PHPExcel_Writer_Excel2007_Workbook-> writeDefinedNames(Object(PHPExcel_Shared_XMLWriter),Object(PHPExcel ))#5 /李 brass / WebServer / Doc in /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php on line 594

public static function coordinateFromString($pCoordinateString = 'A1') { if (preg_match("/^([$]?[AZ]{1,3})([$]?\d{1,7})$/", $pCoordinateString, $matches)) { return array($matches[1],$matches[2]); } elseif ((strpos($pCoordinateString, ':') !== false) || (strpos($pCoordinateString, ',') !== false)) { throw new PHPExcel_Exception('Cell coordinate string can not be a range of cells'); } elseif ($pCoordinateString == '') { throw new PHPExcel_Exception('Cell coordinate can not be zero-length string'); } throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString); // line 594 } 

这是我正在使用的Excel文件 。 想法?

我的PHP代码:

 /** Error reporting */ error_reporting(E_ALL); ini_set('display_errors', TRUE); ini_set('display_startup_errors', TRUE); date_default_timezone_set('America/Los_Angeles'); define('EOL',(PHP_SAPI == 'cli') ? PHP_EOL : '<br />'); date_default_timezone_set('America/Los_Angeles'); /** * PHPExcel * * Copyright (c) 2006 - 2015 PHPExcel * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * @category PHPExcel * @package PHPExcel * @copyright Copyright (c) 2006 - 2015 PHPExcel (http://www.codeplex.com/PHPExcel) * @license http://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt LGPL * @version ##VERSION##, ##DATE## */ /** PHPExcel */ require_once dirname(__FILE__) . '/Classes/PHPExcel.php'; //echo date('H:i:s') , " Load from Excel2007 file" , EOL; $objReader = PHPExcel_IOFactory::createReader("Excel2007"); $objReader->setIncludeCharts(TRUE); $objPHPExcel = $objReader->load("templates/Cue_Sheet_Template.xlsx"); echo date('H:i:s') , " Update cell data values that are displayed in the chart" , EOL; $objWorksheet = $objPHPExcel->getActiveSheet(); /*$objWorksheet->fromArray( array( array(50-12, 50-15, 50-21), array(50-56, 50-73, 50-86), array(50-52, 50-61, 50-69), array(50-30, 50-32, 50), ), NULL, 'B2' );*/ // Save Excel 2007 file echo date('H:i:s') , " Write to Excel2007 format" , EOL; $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007'); $objWriter->setIncludeCharts(TRUE); $objWriter->save(str_replace('.php', '.xlsx', __FILE__)); echo date('H:i:s') , " File written to " , str_replace('.php', '.xlsx', pathinfo(__FILE__, PATHINFO_BASENAME)) , EOL; // Echo memory peak usage echo date('H:i:s') , " Peak memory usage: " , (memory_get_peak_usage(true) / 1024 / 1024) , " MB" , EOL; // Echo done echo date('H:i:s') , " Done writing file" , EOL; echo 'File has been created in ' , getcwd() , EOL; 

编辑(下面的原始答案)

我取得了成功的结果。 我所做的是复制你的xlsx模板文件的内容。 打开一个新的Excel表格并粘贴原始模板中的内容。 我删除了一些定义了一些字段的条件函数,现在我得到了一个体面的结果,你的格式正确。

这是预编译的文件。

在这里输入图像说明

原版的

对不起,这不是你想要的答案。 PHPExcel无法parsing打印范围。

你的解决scheme应该是将你的打印区域重新定义为PHPExcel可以parsing的东西,或者使用正则expression式(即preg_split)为PHPExcel编写自定义parsing器,而不是简单地分割范围值。

你基本上是采取一个Excel表,插入值,并试图输出一个新的Excel表。

如果你注释掉

throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString); // line 594

那么你会得到你的新的Excel表格格式被破坏。

问题出在您的打印区域

 A1:A1,0:0,0:0,(MAX(MATCH(REPT("Z":MAX(MATCH(REPT("Z",29):29),B:B,1):1),MATCH(REPT("Z":MATCH(REPT("Z",29):29),C:C,1):1),MATCH(REPT("Z":MATCH(REPT("Z",29):29),L:L,1):1),MATCH(REPT("Z":MATCH(REPT("Z",29):29),M:M,1):1),MATCH(REPT("Z":MATCH(REPT("Z",29):29),N:N,1):1),MATCH(REPT("Z":MATCH(REPT("Z",29):29),O:O,1))):1))),18):18) 

哪个PHPExcel决定用':'来爆炸,就在'Z'之后。

我试图重新定义您的打印区域,但一直未能。 我现在没时间去试试了。 希望这能让你走向正确的方向。