Wednesday, 14 September 2016

How to read excel file without uploading on server

The following code includes three columns namely as " name , contact , email " as shown the below screenshot



index.php



<form action="upload.php" method="post" enctype="multipart/form-data">
<div class="row">
  <div class="col-lg-2">
  </div>
                <div class="col-lg-8">
                    <div class="panel panel-default">
                       
                        <!-- /.panel-heading -->
                        <div class="panel-body">
                            <div class="table-responsive">
                                <table class="table table-striped table-bordered table-hover">
                                   
                                    <tbody>
                                       
<tr>
                                            <th>Read Excel</th>
<td>
<input type="file" name="file"/> 
</td>
</tr>
<tr>
                                            <th></th>
<td>
<input type="submit" value="Upload"/>
</td>
</tr>
                                            
                                    </tbody>
                                </table>
                            </div>
                            <!-- /.table-responsive -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
             <div class="col-lg-2">
  </div>
            </div>
</form>




upload.php


<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Read Excel File</title>

</head>
<body>

<?php
set_include_path(get_include_path() . PATH_SEPARATOR . 'Classes/');
include 'PHPExcel/IOFactory.php';



// This is the file path to be uploaded.
//$inputFileName = 'discussdesk.xlsx'; 
$inputFileName=$_FILES["file"]["tmp_name"];



try {
$objPHPExcel = PHPExcel_IOFactory::load($inputFileName);
} catch(Exception $e) {
die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
}


$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);  // Here get total count of row in that Excel sheet
?>
<table>
<tr>
<th>Name</th><th>Contact</th><th>Email</th>
</tr>
<?php

for($i=2;$i<=$arrayCount;$i++){
$name = trim($allDataInSheet[$i]["A"]);
$contact=trim($allDataInSheet[$i]["B"]);
$email=trim($allDataInSheet[$i]["C"]);

?>
<tr>
<td><?php echo $name; ?></td>
<td><?php echo $contact; ?></td>
<td><?php echo $email; ?></td>
</tr>
<?php

}

?>
</table>
<body>
</html>


you can also download the zip file of the above code..