Home > php教程 > php手册 > PHPExcel出现Formula Error的解决方案

PHPExcel出现Formula Error的解决方案

WBOY
Release: 2016-06-06 20:09:50
Original
2628 people have browsed it

今天有用户反馈导出excel出现错误,于是看了下错误日志,发现如下报错: 未经允许严禁转载 Fatal error: Uncaught exception PHPExcel_Exception with message excel!J97 - Formula Error: An unexpected error occured in ?xxx\PHPExcel\Cell.php on line 3

今天有用户反馈导出excel出现错误,于是看了下错误日志,发现如下报错:

未经允许严禁转载

Fatal error: Uncaught exception ‘PHPExcel_Exception’ with message ‘excel!J97 -> Formula Error: An unexpected error occured’ in ?xxx\PHPExcel\Cell.php on line 307本文来自leo108's blog

可以看到是在处理J97这个单元格的时候出现了“算式错误”

PHPExcel出现Formula Error的解决方案

于是先从数据库中定位出错的单元格,发现是一个以“=”开头的字符串采集者烂JJ

在excel中一个单元格如果是以“=”开头,则说明这个单元格是根据其他单元格的值算出来的,“=”后面必须跟着一个合法的表达式

未经允许严禁转载

而那个字符串是用户的输入,很明显不应该是一个合法的表达式,所以应该在代码中过滤掉本文来自http://leo108.com

方法很简单,在“=”前面加一个半角的单引号就可以了,所以修改后的代码如下:PHP

if(strpos($value,'=') === 0){
    $value = "'".$value;
}
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($x,$y, $value);
Copy after login

 

PHPExcel

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template