Reading Excel Spreadsheets with PHP

PHP is yet most powerful language when it comes to deal with different data formats like SQL Results, XML Files etc. To improve the functionality of PHP, we use extensions. There are many extensions/packages which let us to read more different types of data formats. Today, we will be using one of them, i.e. phpexcelreader.

PHPExcelReader comes with two files oleread.inc and reader.php. We will need to include reader.php in order to use the package. For the sake of “simplicity”, I have made some changes in example file and data. Grab it here:

Dowload PHPExcelReader.zip

Sample Excel Spreadsheet
Sample Excel Spreadsheet

Close
Sample Excel Spreadsheet

Take a look at example.php, code is pretty much straight forward. As I said before, in order to use the package we need to include reader.php into our code.
require_once('excel/reader.php');
Next, create a new object from the class Spreadsheet_Excel_Reader and give the excel file which you like to read.

$xls = new Spreadsheet_Excel_Reader();
$xls->read('indiastamps_yearsets.xls');

All the rows and columns can be accessed through array variable sheets. We use two for loops to access each and every element of the sheet0.
$xls->sheets[0]['cells'][$row][$column]

Result
Result

Close
Result

If you are Microsoft Excel 2007 user, be sure about saving file in 1997-2003 .xls format else it will not work. Thanks for reading. :)

Tags: , , , , ,

3 Responses to “Reading Excel Spreadsheets with PHP”

  1. ayush Says:

    Nice Thankx :)

  2. jayu Says:

    nice. but problem with date when try to make file in open office.org excel format. when you create file in open office org calc and save it into Microsoft excel 97/2000/XP format. date will give fake information.

  3. Charandeep Singh Says:

    @Jayu
    I think this is happening because, when OpenOffice Export the file in Microsoft Excel .xls format, it replaces original Dates with its Numeric Date Values. Note here, Dates are actually saved as Numeric values in Excel file.

    Eg. Date: 11/12/2009 having a numeric value of 40,129.00

    You can try for some other Excel Exporter Software, or better use Microsoft Office 2003/07.

Leave a Reply