最近遇到的一個bug,根據條件搜索出來的數據,用PHPExcel導出Excel,很奇葩的問題是:只有某一天的數據導出來報錯:找不到文件...
百思不得其解,各個步驟數據都獲取到了,可以打印出來,沒有毛病,後來用fiddler監聽,發現報了一個500的錯誤:PHP Fatal error: Uncaught exception 'PHPExcel_Calculation_Exception' ...
查了一波,大多是說導出的數據中有特殊符號 “=” 導致Excel把等號後面的當成公式處理,所以出錯
可是,問題是數據太大,這個“=”也不好找
在stack overflow上找到了答案:引用一下
I'm guessing that one of the values retrieved from your database begins with an =
sign, in which case PHPExcel will "guess" that it's supposed to be a formula and store it as such.
If you have values that begin with an =
that aren't formulae, then you need to tell PHPExcel that it is a string and that it should be stored as such.
This behaviour is defined in the default cell binder (PHPExcel/Cell/DefaultValueBinder.php
).
If you want to force PHPExcel to store these values as strings, then you can use the setCellValueExplicit()
method (which defaults to enforcing a string)
Or you can write a custom value binder and apply that instead of the default value binder
用setCellValueExplicit()
把所有的輸出到Excel表格的數據強制轉爲字符串,問題就解決了
馬克一下