-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexcel-upload.php
61 lines (45 loc) · 1.67 KB
/
excel-upload.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<?php
/** Set default timezone (will throw a notice otherwise) */
date_default_timezone_set('Asia/Kolkata');
include './PHPExcel/IOFactory.php';
if(isset($_FILES['file']['name'])){
$file_name = $_FILES['file']['name'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
echo "<br>XYZ".$ext;
//Checking the file extension
if($ext == "xlsx"){
$file_name = $_FILES['file']['tmp_name'];
$inputFileName = $file_name;
// Read your Excel workbook
try {
$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objPHPExcel = $objReader->load($inputFileName);
} catch (Exception $e) {
die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME)
. '": ' . $e->getMessage());
}
//Table used to display the contents of the file
echo '<center><table style="width:50%;" border=1>';
// Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
// Loop through each row of the worksheet in turn
for ($row = 1; $row <= $highestRow; $row++) {
// Read a row of data into an array
$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row,
NULL, TRUE, FALSE);
echo "<tr>";
//echoing every cell in the selected row for simplicity. You can save the data in database too.
foreach($rowData[0] as $k=>$v)
echo "<td>".$v."</td>";
echo "</tr>";
}
echo '</table></center>';
}
else{
echo '<p style="color:red;">Please upload file with xlsx extension only</p>';
}
}
?>