How to read excel sheet in php

php

Posted on 09 Sep 15 by Vickey
  1611



For reading excel file in php download the PHPExcel library and include it in your php file where you want to read the sheet

require_once('PHPExcel/IOFactory.php');
require_once('PHPExcel.php');

If you want to read single sheet from a file then you can use the below code

function readSheet($sheet_path){

    $obj_excel = PHPExcel_IOFactory::load($sheet_path);

	$get_sheets='';
    $get_sheetData = "";

 	$sheet = $obj_excel->getSheetByName('Sheet1');
 	$get_sheetData=$sheet->toArray(null,true,true,true);
	echo "<pre>"; print_r($get_sheetData); 
}
readSheet('test.xls');

if you want to read all sheets then you can use

	function readSheet($sheet_path){

	    $obj_excel = PHPExcel_IOFactory::load($sheet_path);

		$get_sheets='';
	    $get_sheetData = array();
	 	$get_sheets = $obj_excel->getAllSheets();
	    foreach($get_sheets as $sheet){

	          $get_sheetData[]=$sheet->toArray(null,true,true,true);
	      
	    }
		echo "<pre>"; print_r($get_sheetData); 
	}

 


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com