php讀取EXCEL檔案 php excelreader讀取excel檔案

來源:互聯網
上載者:User

php開發中肯定會遇到將excel檔案內容匯入到資料庫的需要,php-excel-reader是一個讀取excel的類,可以很輕鬆的使用它讀取excel檔案非常方便。

php-excel-reader: http://www.jb51.net/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';
//建立對象
$data = new Spreadsheet_Excel_Reader();
//設定文本輸出編碼
$data->setOutputEncoding('UTF-8');
//讀取Excel檔案
$data->read("example.xls");
//$data->sheets[0]['numRows']為Excel行數
for ($i = 1; $i <= $data->sheets[0]['numRows']; $i++) {
//$data->sheets[0]['numCols']為Excel列數
for ($j = 1; $j <= $data->sheets[0]['numCols']; $j++) {
//顯示每個儲存格內容
echo $data->sheets[0]['cells'][$i][$j].' ';
}
echo '<br>';
}
?>

讀取結果如下

再來說說這個類的小問題:

(1)出現Deprecated: Function split() is deprecated in 。。。錯誤

解決:將excel_reader2.php源碼中split改為explode,詳情點擊php中explode與split的區別介紹

(2)出現Deprecated: Assigning the return value of new by reference is deprecated in錯誤

解決:將excel_reader2.php源碼中$this->_ole =& new OLERead()中 &去掉,因為php5.3中廢除了=& 符號直接用=引用

(3)亂碼問題解決:

建構函式是function Spreadsheet_Excel_Reader($file='',$store_extended_info=true,$outputEncoding=''),它預設的編碼是utf-8,如果不指定,可能會出現亂碼問題,可通過$data->setOutputEncoding('GBK');指定,還有如果你使用dump()函數,dump()函數將excel內容一html格式輸出,使用htmlentities將字元轉化為html的,它預設使用ISO8559-1編碼的,所以你要將 excel_reader2.php源碼中 htmlentities($val)函數改為htmlentities($val,ENT_COMPAT,"GB2312");才行。

最後來說說,php-excel-reader操作excel中的兩個重要的方法

1.dump(),它可以將excel內容以html格式輸出:

echo $data->dump(true,true);

2.將excel資料存入數組中,使用$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中的資料了,好了,使用php-excel-reader讀取excel檔案就是這麼簡單

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.