今天有用户反馈导出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);
phpexcel