天天看點

PHP-ExcelReader

一、概述

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