Click here to Skip to main content
15,894,017 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

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, Microsoft Excel, XAMPP

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

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 http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900