Skip to content Skip to sidebar Skip to footer
Reading Time: < 1 minute

How can you read and display excel file using php and save that details to the database? You can use this simple php class to do this.That is read row by row while reading column by column.While reading like this it gets each cells details.Look at this example that is simply describes this process and you can understand this better.

First download excel reader.php class file. You can download reader.php file here.
Also you must include this file of this reader.php file.Download
Here is the sample.xls file format.

How to read excel file using php

	Sheet 1:
read('sample.xls'); // set the excel file name here $x=1; while($x<=$excel->sheets[0]['numRows']) { // reading row by row echo "\t\n"; $y=1; while($y<=$excel->sheets[0]['numCols']) {// reading column by column $cell = isset($excel->sheets[0]['cells'][$x][$y]) ? $excel->sheets[0]['cells'][$x][$y] : ''; echo "\t\t\n"; // get each cells values $y++; } echo "\t\n"; $x++; } ?>

This is reading sheet1 details.If you want to read sheet 2 details, then change the sheets[0] to sheets[1] in both while loops.
while($x<=$excel->sheets[0]['numRows']) {
Remember “sheets” is the array.Array begin from 0.You already know about it. 😀
Sheet1 -> sheets[0]
Sheet2 -> sheets[1]
Sheet3 -> sheets[2]

How to read and save details to the database using php

Table structure

DROP TABLE IF EXISTS `users_details`;
CREATE TABLE `users_details` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) default NULL,
  `job` varchar(255) default NULL,
  `email` varchar(255) default NULL,
  PRIMARY KEY  (`id`)

  Save Excel file details to the database
			while($x<=$excel->sheets[0]['numRows']) {
				$name = isset($excel->sheets[0]['cells'][$x][1]) ? $excel->sheets[0]['cells'][$x][1] : '';
				$job = isset($excel->sheets[0]['cells'][$x][2]) ? $excel->sheets[0]['cells'][$x][2] : '';
				$email = isset($excel->sheets[0]['cells'][$x][3]) ? $excel->sheets[0]['cells'][$x][3] : '';
				// Save details
				$sql_insert="INSERT INTO users_details (id,name,job,email) VALUES ('','$name','$job','$email')";
				$result_insert = mysql_query($sql_insert) or die(mysql_error());

That’s only.

Download full source code(20 KB)