php读取excel类——PHP-ExcelReader
生活随笔
收集整理的這篇文章主要介紹了
php读取excel类——PHP-ExcelReader
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
php讀取excel類——PHP-ExcelReader
http://sourceforge.net/projects/phpexcelreader/
一、 概述
PHP-ExcelReader 是一個讀取 Excel xsl 文件內容的一個 PHP 類。
它的下載網址: http://sourceforge.net/projects/phpexcelreader/
文件名: phpExcelReader.zip
包含兩個必需文件: oleread.inc 、 reader.php 。其它文件是一個應用例子 , 自述文件等
二、 文件使用
首先 , 包含 reader 類文件: require_once " reader.php";
新建一個實例: $xl_reader= new Spreadsheet_Excel_Reader ( );
讀取 Excel 文件信息: $xl_reader->read("filename.xls");
它將導出 Excel 文件中所有可以識別的數據存儲在一個對象中。數據存儲在 2 個數組中,目前沒有提供方法 / 函數訪問這些數據. 可以像下面這樣簡單的使用數組名。
sheets 數組包含了讀取入對象的大量數據。它將導出 Excel 文件中所有可以識別的數據存儲在一個 2 維數組中 ? $xl_reader->sheets[x][y] 。 x ? 為文檔中的表序號, y ? 是以下的某個參數 ? :
l ? ? ? ? ?numRows -- int -- ? 表的行數
例如: $rows = $xl_reader->sheets[0]['numRows']
l ? ? ? ? ?numCols -- int -- ? 表的列數
例如: $cols = $xl_reader->sheets[0]['numCols']
l ? ? ? ? ?cells -- array -- ? 表的實際內容。是一個 ? [row][column] 格式的 2 維數組
? 例如: $cell_2_4 = $xl_reader->sheets[0]['cells'][2][4] // 行 2, 列 4 中的數據
l ? ? ? ? ?cellsInfo -- array -- ? 表格中不同數據類型的信息。每個都包含了表格的原始數據和類型。這個數組包含 2 部分: raw -- ? 表格原始數據; type -- ? 數據類型。
注:只顯示非文本數據信息。
例如: $cell_info = $xl_reader[0]['cellsInfo'][2][4]
$cell_info['raw'] is the raw data from the cell
$cell_info['type'] is the data type
$xl_reader->sheets 數組示例:
boundsheets ? 數組包含了對象的其它信息,數組按 workbook 索引。 ? 第二個索引為名稱: $xl_reader->boundsheets[i]['name'] ? 返回第 i 個表的表名
例如: $sheetname = $xl_reader->boundsheets[0]['name']; // name of the first sheet
$xl_reader-> boundsheets 數組示例:
Array
(
? ? ? [0] => Array
? ? ? ? ? (
? ? ? ? ? ? ? [name] => Sheet1
? ? ? ? ? ? ? [offset] => 3054
? ? ? ? ? )
? )
PHP-ExcelReader 只能支持 ? BIFF7 ,BIFF8 格式的文件。包括 Excel95 到 Excel2003. 但是不包含 Excel5.0 及之前的版本. 實際上 ? Excel XP ? 和 Excel 2003 ? 使用的 BIFF8X 是 BIFF8 格式的一個擴展 . 所有添加的特性可能不被 PHP-ExcelReader. 鎖支持。否則它只能以 Excel XP/2003 文件運行。
如果出現: Fatal error: require_once() [function.require]: Failed opening required 'Spreadsheet/Excel/Reader/ OLERead.php ' (include_path='.;\xampp\php\PEAR') in XXXX
意 思是缺少Spreadsheet/Excel/Reader/OLERead.php這個文件。但是確實是沒有這個文件呀!找了找,在excel目錄下發 現了oleread.inc文件,于是將Spreadsheet/Excel/Reader/OLERead.php換成 oleread.inc 就OK了!
也就是將
require_once 'Spreadsheet/Excel/Reader/OLERead.php';
修改為
require_once 'oleread.inc';
即可。
另外,在example.php 中,需要修改
$data->setOutputEncoding('CP1251');
為
$data->setOutputEncoding('CP936');
不然的話中文將會有問題。
如果是使用繁體的話可以修改為CP950、日文是CP932,具體可參考codepage說明。
還有,其自帶的 jxlrwtest.xls 可能有問題,需要修改example.php中的:
$data->read(' jxlrwtest.xls ');
總結
以上是生活随笔為你收集整理的php读取excel类——PHP-ExcelReader的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP的mysqli扩展
- 下一篇: PHP读取excel表格内容 PHP-E