使用 PHPExcel 编辑 excel 文件
Edit excel file with PHPExcel
我正在尝试使用 PHPExcel 编辑 excel 文件,但我的最终文件已损坏。我的猜测是因为文件太复杂了,但我不确定。一个非常简单的 excel 文件可以正常工作。
首先,PHPExcel 读取文件,更改数据然后重新创建,对吗?
不就是打开和写入同一个文件吗?
我收到此错误消息:
Fatal error: Uncaught exception 'PHPExcel_Exception' with message
'Invalid cell coordinate (MAX(MATCH(REPT("Z"' in
/Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php:594 Stack
trace: #0
/Library/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 /Library/WebServer/Doc in
/Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php on line 594
public static function coordinateFromString($pCoordinateString = 'A1')
{
if (preg_match("/^([$]?[A-Z]{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
}
This is the excel file我在用。想法?
我的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 sheet 并粘贴原始模板中的内容。我删除了一些定义一些字段的条件函数,现在我得到了一个不错的结果,你的格式就位了。
原版
抱歉,这不是您想要的答案。 PHPExcel 无法解析打印范围。
您的解决方案应该是将您的打印区域重新定义为 PHPExcel 可以解析的内容,或者使用不简单地拆分范围值的正则表达式(即 preg_split)为 PHPExcel 编写您自己的自定义解析器。
您基本上是在使用 excel sheet,插入值并尝试输出新的 excel sheet.
如果你注释掉
throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString); // line 594
然后你会得到你的新 excel sheet 格式被毁了。
问题出在您的打印区域
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)
'Z'.
之后的“:”决定了哪个 PHPExcel 爆炸
我尝试重新定义您的打印区域,但未能成功。我现在没时间尝试这个。希望这能让你朝着正确的方向前进。
我正在尝试使用 PHPExcel 编辑 excel 文件,但我的最终文件已损坏。我的猜测是因为文件太复杂了,但我不确定。一个非常简单的 excel 文件可以正常工作。
首先,PHPExcel 读取文件,更改数据然后重新创建,对吗? 不就是打开和写入同一个文件吗?
我收到此错误消息:
Fatal error: Uncaught exception 'PHPExcel_Exception' with message 'Invalid cell coordinate (MAX(MATCH(REPT("Z"' in /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php:594 Stack trace: #0 /Library/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 /Library/WebServer/Doc in /Library/WebServer/Documents/sb/Classes/PHPExcel/Cell.php on line 594
public static function coordinateFromString($pCoordinateString = 'A1')
{
if (preg_match("/^([$]?[A-Z]{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
}
This is the excel file我在用。想法?
我的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 sheet 并粘贴原始模板中的内容。我删除了一些定义一些字段的条件函数,现在我得到了一个不错的结果,你的格式就位了。
原版
抱歉,这不是您想要的答案。 PHPExcel 无法解析打印范围。
您的解决方案应该是将您的打印区域重新定义为 PHPExcel 可以解析的内容,或者使用不简单地拆分范围值的正则表达式(即 preg_split)为 PHPExcel 编写您自己的自定义解析器。
您基本上是在使用 excel sheet,插入值并尝试输出新的 excel sheet.
如果你注释掉
throw new PHPExcel_Exception('Invalid cell coordinate '.$pCoordinateString); // line 594
然后你会得到你的新 excel sheet 格式被毁了。
问题出在您的打印区域
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)
'Z'.
之后的“:”决定了哪个 PHPExcel 爆炸我尝试重新定义您的打印区域,但未能成功。我现在没时间尝试这个。希望这能让你朝着正确的方向前进。