Click here to Skip to main content
14,601,061 members

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)?

Sumate Mephokkij asked:

Open original thread
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
Tags: Javascript, HTML (HTML5), PHP, XAMPP, Excel

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the The Code Project Open License (CPOL).




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100