Reading Excel Documents from PHP applications  

Check out latest Mobile Phones and Books only at Flipkart.com
Flipkart.com

In this article we will learn on how we can read Microsoft Excel Sheet in PHP. To achieve this we will be using Open Source Tool PHPExcelReader. It provides us with necessary API that allow us to read the Excel Sheet in PHP.

Directory Structure for ExcelReader:
When you will unzip the excelreader, you will find example.php that shows us how to read excel sheet using php code. And the most important thing is the Excel folder, it contains all the necessary files and API that will actually perform your operation. Keep the Excel folder as it is as files are included in the API code as per the directory structure.

Implement Text to Speech in PHP

Example:
Well the included example.php is sufficient to understand the functionality of the ExcelReader. But i am showing the same code here with slight modification.

< ?php
 
require_once 'Excel/reader.php';
$data = new Spreadsheet_Excel_Reader();
 
// Set output Encoding.
$data->setOutputEncoding('CP1251');
 
/***
* if you want you can change 'iconv' to mb_convert_encoding:
* $data->setUTFEncoder('mb');
*
**/
 
/***
* By default rows & cols indeces start with 1
* For change initial index use:
* $data->setRowColOffset(0);
*
**/
 
/***
*  Some function for formatting output.
* $data->setDefaultFormat('%.2f');
* setDefaultFormat - set format for columns with unknown formatting
*
* $data->setColumnFormat(4, '%.3f');
* setColumnFormat - set format for column (apply only to number fields)
*
**/
 
$data->read('test.xls');	//Passing the excel sheet to be read from PHP
error_reporting(E_ALL ^ E_NOTICE);
 
for ($i = 0; $i < = $data->sheets[0]['numRows']; $i++) {
	echo $data->sheets[0]['cells'][$i][1]."<br />";
}
 
?>

HTML Scrapper in PHP

The most important thing here is:

DescriptionSource Code
Accessing Specific Sheet object inside excel document$data->sheets[0](Accessing 1st sheet)
Accessing total rows inside specific sheet $data->sheets[0]['numRows']
Accessing individual cells data $data->sheets[0]['cells'][0][1] (Here i am accessing 1st sheet cell[0][1] i.e. A1 cell)

If we know this basic code than we can read the data inside the excel sheet from php. Happy Programming :)


Related Articles:

Categories: PHP Tags: ,
  1. Gavhot
    September 22nd, 2009 at 07:15 | #1

    Is there anyway display the sheet name?

  2. sharath
    April 22nd, 2010 at 06:54 | #2

    hello,
    Is there a way to retrieve sheet names using reader.php
    thanx,
    sharath.

  3. Alex
    November 8th, 2010 at 08:52 | #3

    Hello everyone :)

    I use PHPExcelReader but now I got a problem :(
    Look at http://www.codifica2.com/oscar/test_excel.php
    It seems like the camps were incomplete.

    You can download the excel file at http://www.codifica2.com/oscar/rpts/rptDocenteControlAsistenciaC2-3.xls

    My code is:
    $excel_reader = new Spreadsheet_Excel_Reader();
    $excel_reader->setUTFEncoder(‘iconv’);
    $excel_reader->setOutputEncoding(‘CP1251′);
    $excel_reader->read(\rpts/rptDocenteControlAsistenciaC2-3.xls\);
    $excel_reader->sheets[x][y];

    for ($fila=13; $filasheets[0]['numRows']; $fila++) {
    echo $excel_reader->sheets[0]['cells'][$fila][1] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][2] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][3] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][4] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][5] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][6] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][7] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][8] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][9] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][10] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][11] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][12] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][13] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][14] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][15] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][16] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][17] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][18] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][19] . \ / \;
    echo $excel_reader->sheets[0]['cells'][$fila][20] . \\;
    }
    ?>

    Thank you very much!

  4. jhoney
    November 21st, 2010 at 21:49 | #4

    Hello dears,

    i have the same problem, some special characters apears in db and on the page,

    Need a bit assistance..

  1. October 16th, 2009 at 00:25 | #1
  2. October 16th, 2009 at 07:21 | #2
  3. March 12th, 2010 at 07:48 | #3
  4. May 21st, 2010 at 07:34 | #4
  5. June 16th, 2010 at 14:27 | #5
  6. November 12th, 2010 at 05:21 | #6
  7. March 3rd, 2011 at 03:17 | #7

 

Page optimized by WP Minify WordPress Plugin