2007年3月8日 星期四

好用的 php 讀取 excel 套件 !!

PHP-ExcelReader
Overview

PHP-ExcelReader is a PHP class for reading the contents of Excel xls files. It used to require the OLE package but does not need to run on a windows platform. You could also find the OLE package here.

Starting before version 2i, the oleread.inc file is included with the PHP ExcelReader distribution, so it is no longer necessary to have the OLE package available.
Installation

The download consists of a number of files, but only two, reader.php and oleread.inc, are necessary to run the ExcelReader. The other files are an example application, readme, etc.

All that is required to run the class is to include the file with the class (reader.php by default) in your project. This, in turn, includes oleread.inc.
Usage

First, include the reader class file:

include('reader.php');

Make an instance of the class:

$xl_reader = new Spreadsheet_Excel_Reader();

Next we tell the object what file to read

$xl_reader->read("filename.xls");

This extracts all of the recognized data from the Excel file and stores it in an object.

The data is stored in two arrays. There are no methods/functions to access the data. Simply use the array names as described below.

The sheets array houses the bulk of the data that is read into the object

The data is stored in a 2D array:

$xl_reader->sheets[x][y]

* x is the sheet number in the document
* y is one of the following properties:
o numRows -- int -- number of rows on the sheet
example:

$rows = $xl_reader->sheets[0]['numRows']

o numCols -- int -- number of columns on the sheet
example:

$cols = $xl_reader->sheets[0]['numCols']

o cells -- array -- the actual information from the sheet. This is a 2D array in the form of [row][column]
example:

$cell_2_4 = $xl_reader->sheets[0]['cells'][2][4] //the data from the cell at row 2, column 4

This is correct, but not very readable, so just assign the cells array to a variable and reference from there:

$cells = $xl_reader[0]['cells']; //the array of cell information
$cell_2_4 = $cells[2][4]; //the data from the cell at row 2, column 4

o cellsInfo -- array -- the information about the different data types of the cells. For each cell this shows the raw data in the cell, and the type of data it is on the sheet.
This is an array with two parts:
raw -- the raw data in the cell
type -- the data type of the data in the cell
Note: This only displays information about non-text data.

example:

$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

[It would be nice to have more info here on how to interpret the raw and type data.]

The boundsheets array houses the remainder of the data in the object. This array is indexed by the sheet number within the workbook. The second index is always name.

$xl_reader->boundsheets['name']

gives the name of the ith sheet in the document.

example:

$sheetname = $xl_reader->boundsheets[0]['name']; // name of the first sheet






下載 :
http://sourceforge.net/project/showfiles.php?group_id=99160

【下列文章您可能也有興趣】

沒有留言: