Friday, 29 November 2013

Reading Excel Files In PHP

In this tutorial I’m going to show you how you can use the php library called PHP Excel Reader to read excel files using php. Yes, things like excel readers for php has already been written by smart people in the past so I’m not going to show you how to build an excel reader from scratch. It’s not quite my level yet, I still consider myself a beginner. Anyway, let’s get to the main topic.


Index.php

<?php
include 'excel_reader.php';       // include the class
$excel = new PhpExcelReader;      // creates object instance of the class
$excel->read('data2.xls');   // reads and stores the excel file data

// Test to see the excel data stored in $sheets property
/*echo '<pre>';
var_export($excel->sheets);
echo '</pre>';
*/$rows=$excel->sheets[0]['numRows'];
$cols=$excel->sheets[0]['numCols'];
echo "<table>";
for($i=2;$i<$rows;$i++)
{
echo "<tr>";
for($j=1;$j<$rows;$j++)
{
echo "<td>".$excel->sheets[0]['cells'][$i][$j]."</td>";
//echo $excel->sheets[0]['cells'][$i][$j];
}
echo "</tr>";
}
echo "</table>";
?>

0 comments:

Post a Comment