當(dāng)前位置:
首頁(yè) >
php读取excel类——PHP-ExcelReader
發(fā)布時(shí)間:2025/5/22
60
豆豆
生活随笔
收集整理的這篇文章主要介紹了
php读取excel类——PHP-ExcelReader
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
php讀取excel類——PHP-ExcelReader
http://sourceforge.net/projects/phpexcelreader/
一、 概述
PHP-ExcelReader 是一個(gè)讀取 Excel xsl 文件內(nèi)容的一個(gè) PHP 類。
它的下載網(wǎng)址: http://sourceforge.net/projects/phpexcelreader/
文件名: phpExcelReader.zip
包含兩個(gè)必需文件: oleread.inc 、 reader.php 。其它文件是一個(gè)應(yīng)用例子 , 自述文件等
二、 文件使用
首先 , 包含 reader 類文件: require_once " reader.php";
新建一個(gè)實(shí)例: $xl_reader= new Spreadsheet_Excel_Reader ( );
讀取 Excel 文件信息: $xl_reader->read("filename.xls");
它將導(dǎo)出 Excel 文件中所有可以識(shí)別的數(shù)據(jù)存儲(chǔ)在一個(gè)對(duì)象中。數(shù)據(jù)存儲(chǔ)在 2 個(gè)數(shù)組中,目前沒有提供方法 / 函數(shù)訪問這些數(shù)據(jù). 可以像下面這樣簡(jiǎn)單的使用數(shù)組名。
sheets 數(shù)組包含了讀取入對(duì)象的大量數(shù)據(jù)。它將導(dǎo)出 Excel 文件中所有可以識(shí)別的數(shù)據(jù)存儲(chǔ)在一個(gè) 2 維數(shù)組中 ? $xl_reader->sheets[x][y] 。 x ? 為文檔中的表序號(hào), y ? 是以下的某個(gè)參數(shù) ? :
l ? ? ? ? ?numRows -- int -- ? 表的行數(shù)
例如: $rows = $xl_reader->sheets[0]['numRows']
l ? ? ? ? ?numCols -- int -- ? 表的列數(shù)
例如: $cols = $xl_reader->sheets[0]['numCols']
l ? ? ? ? ?cells -- array -- ? 表的實(shí)際內(nèi)容。是一個(gè) ? [row][column] 格式的 2 維數(shù)組
? 例如: $cell_2_4 = $xl_reader->sheets[0]['cells'][2][4] // 行 2, 列 4 中的數(shù)據(jù)
l ? ? ? ? ?cellsInfo -- array -- ? 表格中不同數(shù)據(jù)類型的信息。每個(gè)都包含了表格的原始數(shù)據(jù)和類型。這個(gè)數(shù)組包含 2 部分: raw -- ? 表格原始數(shù)據(jù); type -- ? 數(shù)據(jù)類型。
注:只顯示非文本數(shù)據(jù)信息。
例如: $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 數(shù)組示例:
boundsheets ? 數(shù)組包含了對(duì)象的其它信息,數(shù)組按 workbook 索引。 ? 第二個(gè)索引為名稱: $xl_reader->boundsheets[i]['name'] ? 返回第 i 個(gè)表的表名
例如: $sheetname = $xl_reader->boundsheets[0]['name']; // name of the first sheet
$xl_reader-> boundsheets 數(shù)組示例:
Array
(
? ? ? [0] => Array
? ? ? ? ? (
? ? ? ? ? ? ? [name] => Sheet1
? ? ? ? ? ? ? [offset] => 3054
? ? ? ? ? )
? )
PHP-ExcelReader 只能支持 ? BIFF7 ,BIFF8 格式的文件。包括 Excel95 到 Excel2003. 但是不包含 Excel5.0 及之前的版本. 實(shí)際上 ? Excel XP ? 和 Excel 2003 ? 使用的 BIFF8X 是 BIFF8 格式的一個(gè)擴(kuò)展 . 所有添加的特性可能不被 PHP-ExcelReader. 鎖支持。否則它只能以 Excel XP/2003 文件運(yùn)行。
如果出現(xiàn): 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這個(gè)文件。但是確實(shí)是沒有這個(gè)文件呀!找了找,在excel目錄下發(fā) 現(xiàn)了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');
不然的話中文將會(huì)有問題。
如果是使用繁體的話可以修改為CP950、日文是CP932,具體可參考codepage說(shuō)明。
還有,其自帶的 jxlrwtest.xls 可能有問題,需要修改example.php中的:
$data->read(' jxlrwtest.xls ');
總結(jié)
以上是生活随笔為你收集整理的php读取excel类——PHP-ExcelReader的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PHP的mysqli扩展
- 下一篇: PHP读取excel表格内容 PHP-E