程序開發中經常會需要我們對office進行操作,比如用戶信息需要通過excel導入到數據庫。在開源的excel操作類中,有兩款產品最受人青睞,一款是phpExcelReader,一款是phpExcel。
通過phpExcelReader讀取excel的時候,往往會出現以下各種問題:
1、Deprecated: Assigning the return value of new by reference is deprecated
in Assigning the return value of new by reference is deprecated in…
2、Warning: require_once(Spreadsheet/Excel/Reader/OLERead.php)
[function.require-once]: failed to open stream: No such file or
directory in…
3、Notice: iconv() [function.iconv]: Detected an illegal character in input
string in D:wampwwwphpExcelReaderExceleader.php on line…
4、Fatal error: Maximum execution time of 30 seconds exceeded in …
5、The filename *.xls is not readable
現在我針對以上問題一一解答,給出解決方案
針對第一個報錯:
這個是php5.3以後版本的問題,php5.3以下版本不會出現。php5.3開始後,廢除了php中的”=&”符號,所以直接用”=”引用即可。
也就是將
$this->_ole =& new OLERead(); // 大約在reader.php第261行
改爲:$this->_ole = new OLERead();
第二個報錯修改方法:
官方給的phpExcelReader/Excel文件夾中有兩個文件,一個是oleread.inc;一個是reader.php。所以只需要將
require_once ‘Spreadsheet/Excel/Reader/OLERead.php’;
改爲:require_once ‘oleread.inc’就行了,同時還要注意,linux系統中文件夾大小寫也需要統一。
第三個報錯的解決方法:
這個錯誤是用官方給的example裏面的編碼問題,修改方法是:
將example.php裏面的
$data->setOutputEncoding(‘CP1251’);
改爲:
第四個報錯的解決方法:
demo裏面的這個excel文件有錯誤,自己創建一個就OK了。
第五個報錯的解決方法:
最後這個問題有幾種可能,按照“The filename *.xls is not readable”英文字面意思是權限問題。如果是權限問題,請給.xls的文件設置777權限,方法不用說了。
大部分開發者看到這個問題都會認爲就是權限問題,其實不然。如果權限沒問題還報這個錯誤,那麼問題就出在路徑上了,很多人都是用的類似這樣的路徑:/upload/newfile/1447921707/wanhui_user_ipro_1/wanhui_user_ipro/user_exp.xls。在操作文件的時候,這樣的路徑是不行的,路徑前面需要添加網站站點的物理路徑。
解決方法:
假如/www/web/wanhui/abc/public_html/是網站的物理路徑,那麼
裏面的$contentfilename就應該是:
$contentfilename=/www/web/wanhui/abc/public_html/upload/newfile/1447921707/wanhui_user_ipro_1/wanhui_user_ipro/user_exp.xls;