Wednesday, March 3, 2010

PHP Excel Reader

PHP Excel Reader

I recently created a project on Google Code for PHP Excel Reader. This is a PHP library for reading MS Office Excel files that has existed on Sourceforge for quite a while but was no longer being maintained or enhanced. I needed to use it for a work project, so I put in some time to fix it and enhance it to make it more usable. I decided to put it up on Google Code in the hopes that others would find it useful.

Example

Microsoft Excel

require_once 'excel_reader2.php';
$xls = new Spreadsheet_Excel_Reader("example.xls");
echo '';
for ($row=1;$row<=$xls->rowcount();$row++) {
echo '
';
for ($col=1;$col<=$xls->colcount();$col++) {
echo '
';
}
echo '
';
}
echo '
' . $xls->val($row,$col) . '
'
;
?>
HTML Output

Usage

$data = new Spreadsheet_Excel_Reader("test.xls"); 

Retrieve formatted value of cell (first or only sheet): 

  $data->val($row,$col) 

Or using column names: 

  $data->val(10,'AZ') 

From a sheet other than the first: 

  $data->val($row,$col,$sheet_index) 

Retrieve cell info: 

  $data->type($row,$col);
  $data->raw($row,$col);
  $data->format($row,$col);
  $data->formatIndex($row,$col); 

Get sheet size: 

  $data->rowcount();
  $data->colcount(); 

$data->sheets[0]['cells'][$i][$j] - data from $i-row $j-column 

$data->sheets[0]['numRows'] - count rows
$data->sheets[0]['numCols'] - count columns 

$data->sheets[0]['cellsInfo'][$i][$j] - extended info about cell
$data->sheets[0]['cellsInfo'][$i][$j]['type'] = "date" | "number" | "unknown"
$data->sheets[0]['cellsInfo'][$i][$j]['raw'] = value if cell without format
$data->sheets[0]['cellsInfo'][$i][$j]['format'] = Excel-style Format string of cell
$data->sheets[0]['cellsInfo'][$i][$j]['formatIndex'] = The internal Excel index of format 

$data->sheets[0]['cellsInfo'][$i][$j]['colspan']
$data->sheets[0]['cellsInfo'][$i][$j]['rowspan']

2 comments:

  1. Hi Bro

    im doing a php page which is retrieving data from xls sheet and its
    works well the xls sheet having 150 records. its limited to 20 records
    per page to show on result page .there is a search box and button to
    search some specific data , the search parameter giving the result
    from first page result only. i can't get result from full excel sheet.

    $searchparameter=$_GET['searchparameter'];
    $totalrow=$data->rowcount($sheet_index=0);
    $rows=20;
    if ($_GET['page']!="")
    $page=$_GET['page'];

    else
    $page=1;
    echo 'table';



    $count=0;
    $recordno=1;
    for($i=0;$i<$totalrow-6;$i++)
    {

    if((($page-1)*$rows)<=$i && $i<=($page*$rows))
    {
    //$data1=$data->val($startno+$i,1);
    $data2=$data->val($startno+$i,2);
    $data3=$data->val($startno+$i,3);
    $data4=$data->val($startno+$i,6);


    if((stristr($data2,$searchparameter) ||
    stristr($data3,$searchparameter)|| stristr($data4,$searchparameter))||
    $searchparameter=="")
    {
    echo 'table';
    }


    i dont understan whats wrong here, y can't search full records from xls sheet.
    im expecting some ones help to solve my problem.
    thnxx..

    ReplyDelete
  2. Let me review the code and give comments on this Bro.. Thanks..

    ReplyDelete