php讀取EXCEL文件 php excelreader讀取excel文件
php開發(fā)中肯定會遇到將excel文件內容導入到數(shù)據(jù)庫的需要,php-excel-reader是一個讀取excel的類,可以很輕松的使用它讀取excel文件非常方便。
php-excel-reader下載地址: http://www.dbjr.com.cn/codes/67223.html
我下載的是php-excel-reader-2.21版本,使用的時候還遇到幾個小問題,后面再細說,先奉上php實例:
我使用的excel如下圖:
php代碼如下:
<?php
/*by www.phpddt.com*/
header("Content-Type:text/html;charset=utf-8");
require_once 'excel_reader2.php';
//創(chuàng)建對象
$data = new Spreadsheet_Excel_Reader();
//設置文本輸出編碼
$data->setOutputEncoding('UTF-8');
//讀取Excel文件
$data->read("example.xls");
//$data->sheets[0]['numRows']為Excel行數(shù)
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
//$data->sheets[0]['numCols']為Excel列數(shù)
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
//顯示每個單元格內容
echo $data->sheets[0]['cells'][$i][$j].' ';
}
echo '<br>';
}
?>
讀取結果截圖如下:
再來說說這個類的小問題:
(1)出現(xiàn)Deprecated: Function split() is deprecated in 。。。錯誤
解決:將excel_reader2.php源碼中split改為explode,詳情點擊php中explode與split的區(qū)別介紹
(2)出現(xiàn)Deprecated: Assigning the return value of new by reference is deprecated in錯誤
解決:將excel_reader2.php源碼中$this->_ole =& new OLERead()中 &去掉,因為php5.3中廢除了=& 符號直接用=引用
(3)亂碼問題解決:
構造函數(shù)是function Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding=''),它默認的編碼是utf-8,如果不指定,可能會出現(xiàn)亂碼問題,可通過$data->setOutputEncoding('GBK');指定,還有如果你使用dump()函數(shù),dump()函數(shù)將excel內容一html格式輸出,使用htmlentities將字符轉化為html的,它默認使用ISO8559-1編碼的,所以你要將 excel_reader2.php源碼中 htmlentities($val)函數(shù)改為htmlentities($val,ENT_COMPAT,"GB2312");才行。
最后來說說,php-excel-reader操作excel中的兩個重要的方法:
1.dump(),它可以將excel內容以html格式輸出:
echo $data->dump(true,true);
2.將excel數(shù)據(jù)存入數(shù)組中,使用$data->sheets,打印下如下:
Array
(
[0] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 5
[numCols] => 4
[cells] => Array
(
[1] => Array
(
[1] => 編號
[2] => 姓名
[3] => 年齡
[4] => 學號
)
[2] => Array
(
[1] => 1
[2] => 小紅
[3] => 22
[4] => a1000
)
[3] => Array
(
[1] => 2
[2] => 小王
[3] => 33
[4] => a1001
)
[4] => Array
(
[1] => 3
[2] => 小黑
[3] => 44
[4] => a1002
)
[5] => Array
(
[2] => by
[3] => www.phpddt.com
)
)
[cellsInfo] => Array
(
[1] => Array
(
[1] => Array
(
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[2] => Array
(
[1] => Array
(
[string] => 1
[raw] => 1
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 22
[raw] => 22
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[3] => Array
(
[1] => Array
(
[string] => 2
[raw] => 2
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[2] => Array
(
[xfIndex] => 23
)
[3] => Array
(
[string] => 33
[raw] => 33
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 6
[formatColor] =>
[xfIndex] => 23
)
[4] => Array
(
[xfIndex] => 23
)
)
[4] => Array
(
[1] => Array
(
[string] => 3
[raw] => 3
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[string] => 44
[raw] => 44
[rectype] => unknown
[format] => %s
[formatIndex] => 0
[fontIndex] => 0
[formatColor] =>
[xfIndex] => 15
)
[4] => Array
(
[xfIndex] => 15
)
)
[5] => Array
(
[2] => Array
(
[xfIndex] => 15
)
[3] => Array
(
[xfIndex] => 24
[hyperlink] => Array
(
[flags] => 23
[desc] => www.phpddt.com
[link] => http://www.phpddt.co
)
)
)
)
)
[1] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
[2] => Array
(
[maxrow] => 0
[maxcol] => 0
[numRows] => 0
[numCols] => 0
)
)
這樣你應該知道怎么取excel中的數(shù)據(jù)了,好了,使用php-excel-reader讀取excel文件就是這么簡單
- PHP讀取Excel內的圖片(phpspreadsheet和PHPExcel擴展庫)
- 利用phpExcel實現(xiàn)Excel數(shù)據(jù)的導入導出(全步驟詳細解析)
- PHPExcel讀取Excel文件的實現(xiàn)代碼
- PHP導入Excel到MySQL的方法
- php導入導出excel實例
- ThinkPHP使用PHPExcel實現(xiàn)Excel數(shù)據(jù)導入導出完整實例
- php中使用PHPExcel讀寫excel(xls)文件的方法
- php生成excel文件的簡單方法
- phpExcel導出大量數(shù)據(jù)出現(xiàn)內存溢出錯誤的解決方法
- php中導出數(shù)據(jù)到excel時數(shù)字變?yōu)榭茖W計數(shù)的解決方法
- PHP上傳Excel文件導入數(shù)據(jù)到MySQL數(shù)據(jù)庫示例
- 基于PHPExcel的常用方法總結
- PHP使用PhpSpreadsheet操作Excel實例詳解
相關文章
PHP全概率運算函數(shù)(優(yōu)化版) Webgame開發(fā)必備
PHP全概率運算函數(shù)(優(yōu)化版) Webgame開發(fā)必備,需要的朋友可以參考下。2011-07-07Codeigniter+PHPExcel實現(xiàn)導出數(shù)據(jù)到Excel文件
PHPExcel是用來操作OfficeExcel文檔的一個PHP類庫,Codeigniter是一個功能強大的PHP框架。二者結合就能起到非常棒的效果,需要的朋友可以參考下2014-06-06淺析echo(),print(),print_r(),return之間的區(qū)別
這篇文章主要是對echo(),print(),print_r(),return之間的區(qū)別進行了詳細的分析介紹,需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11