How to coding to support import Excel file to auto insert Excel data into PHPExcel+HTML input form (parse this response Excel data into desired fields)?
Because I try coding to
1. send file to PHP.
2. parse EXCEL file with third-party library.
3. create response for AJAX/POST into HTML page.
but can't to parse this response Excel data into desired fields , I have send file to php and autofill Excel data with same id to HTML input tag (example : autofill input id="fname" to Hello and autofill input id="lname" to World).
What I have tried:
Sample code and file at the bottom.
1. excelimport.php
<pre><!DOCTYPE html>
<html>
<head>
<style>
h2 {display: inline;}
</style>
<script>
var _validFileExtensions = [".xls", ".xlsx", ".csv"];
function ValidateSingleInput(oInput) {
if (oInput.type == "file") {
var sFileName = oInput.value;
if (sFileName.length > 0) {
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length,
sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase()) {
blnValid = true;
break;
}
}
if (!blnValid) {
alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(",
"));
oInput.value = "";
return false;
}
}
}
return true;
}
</script>
</head>
<body>
<?php
if(isset($_FILES['excel']) && $_FILES['excel']['error']==0) {
require_once "PHPExcel/Classes/PHPExcel.php";
$tmpfname = $_FILES['excel']['tmp_name'];
$excelReader = PHPExcel_IOFactory::createReaderForFile($tmpfname);
$excelObj = $excelReader->load($tmpfname);
$worksheet = $excelObj->getSheet(0);
$lastRow = $worksheet->getHighestRow();
echo "<table class=\"table table-sm\">";
for ($row = 1; $row <= $lastRow; $row++) {
echo "<tr><td scope=\"row\">";
echo $worksheet->getCell('A'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('B'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('C'.$row)->getValue();
echo "</td><td>";
echo $worksheet->getCell('D'.$row)->getValue();
echo "</td><tr>";
}
echo "</table>";
}
?>
<form action = "" method = "POST" enctype = "multipart/form-data">
<h2 for="myfile1">Select files : </h2>
<input type = "file" name = "excel" onchange="ValidateSingleInput(this)" />
<input type = "submit"/><br><br>
</form>
<h2 for="fname">First name : </h2><input type="text" id="fname" name="fname"
value=""><br><br>
<h2 for="lname">Last name : </h2><input type="text" id="lname" name="lname"
value=""><br><br>
<input type="submit" name="submit2">
</body>
</html>
2. excelimport.xlsx -
http://doanga2007.github.io/excelimport.xlsx
3. PHPExcel with download file at
https://github.com/PHPOffice/PHPExcel/ and I have file structure to image at bottom.
https://i.imgur.com/I45WwXk.png
Screenshot.
https://i.imgur.com/bNhop9T.png