Friday, September 11, 2015

Read Excel file and save details to database using PHP

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 oleread.inc file of this reader.php file.Download oleread.inc.
Here is the sample.xls file format.
sample.xls-format

How to read excel file using php

<html>
  <head>
  </head>
  <body>
<?php
include 'reader.php';
    $excel = new Spreadsheet_Excel_Reader();
?>
Sheet 1:<br/>
    <table border="1">
<?php
        $excel->read('sample.xls'); // set the excel file name here   
        $x=1;
        while($x<=$excel->sheets[0]['numRows']) { // reading row by row 
          echo "\t<tr>\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<td>$cell</td>\n";  // get each cells values
            $y++;
          }  
          echo "\t</tr>\n";
          $x++;
        }
        ?>    
    </table>
  </body>
</html>

Note:
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.
Example:
while($x<=$excel->sheets[0]['numRows']) {
Remember “sheets” is the array.Array begin from 0.You already know about it. ðŸ˜€
So,
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`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=latin1;
<html>
  <head> 
  <title>Save Excel file details to the database</title>
  </head>
  <body>
<?php
include 'db_connection.php';
include 'reader.php';
    $excel = new Spreadsheet_Excel_Reader();
?>
   <table border="1">
<?php
            $excel->read('sample.xls');    
$x=2;
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()); 
   $x++;
}
        ?>    
    </table>
  </body>

No comments:

Post a Comment

Please Comment Here!

How to backup and download Database using PHP

< ?php $mysqlUserName = 'databaseusername' ; $mysqlPassword = 'databasepassword' ; $mysqlHostNa...