PHP-ExcelReader

来源:互联网 发布:淘宝网官方网站 编辑:程序博客网 时间:2024/03/29 21:34
一、概述

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数组示例:

Array

(

    [0] => Array

        (

            [maxrow] => 0

            [maxcol] => 0

            [numRows] => 30

            [numCols] => 12

            [cells] => Array

                (

                    [1] => Array

                        (

                            [1] => 日期

                            [2] => 捐款人姓名/职称

                            [3] => 金额

                            [4] => 原捐款金额

                            [5] => 收据号

                            [6] => 收据抬头

                            [7] => 性别

                            [8] => 用途

                            [9] => 地址

                            [10] => 联系人/电话

                            [11] => 执行情形

                            [12] => 备注

                        )

 

                    [2] => Array

                        (

                            [1] => 2007/06/02

                            [2] => 秦荣华 魏清莲 /集團

                            [3] => 300000

                            [4] => 45

                            [5] => 502

                            [6] => 宁波国雅机械有限公司

                            [7] => 夫婦

                            [8] => 测试

                            [10] => 胡爱华秘书/135 6651 8117

                            [11] => 2008-9愛心小學1所25萬  2008-12愛心小學1所25萬 2009-9 珍珠班2班 750000元  餘額 25萬

                            [12] => 备注

                        )

 

                )

 

            [cellsInfo] => Array

                (

                    [2] => Array

                        (

                            [1] => Array

                                (

                                    [raw] => 1180742400

                                    [type] => date

                                )

 

                            [3] => Array

                                (

                                    [raw] => 300000

                                    [type] => number

                                )

 

                            [4] => Array

                                (

                                    [raw] => 45

                                    [type] => unknown

                                )

 

                            [5] => Array

                                (

                                    [raw] => 502

                                    [type] => unknown

                                )

                         )

                 )

         )

     [1] => Array

        (

            [maxrow] => 0

            [maxcol] => 0

            [numRows] => 0

            [numCols] => 0

        )

 

    [2] => Array

        (

            [maxrow] => 0

            [maxcol] => 0

            [numRows] => 0

            [numCols] => 0

        )

 )

 

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

        )

 

    [1] => Array

        (

            [name] => Sheet2

            [offset] => 6147

        )

 

    [2] => Array

        (

            [name] => Sheet3

            [offset] => 6410

        )

 )

 

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');

附件: phpexcelreader.zip (21.02 K, 下载次数:37)

Tags: excel
原创粉丝点击