Skip to content Skip to sidebar Skip to footer

How To Parse Response Table With Phpexcel Import Excel File To Input Tag (example : Autofill Input Id="fname" To Hello And Input Id="lname" To World)?

How to parse this response table with PHPExcel import Excel file into desired input fields (example : autofill input id='fname' to Hello and autofill input id='lname' to World)? I

Solution 1:

Good news : I have answer to parse response table with PHPExcel import Excel file to input tag (example : autofill input id=“fname” to Hello and input id=“lname” to World) with my full source code.

I have answer credit with link.

  1. excelimport.xlsx (Excel file)

Download Link

  1. excelimport.php (PHP Code)
<!DOCTYPE html><html><head><metacharset="UTF-8"><title>Document</title><style>h2 {display: inline;}
    </style></head><body><formaction=""method="post"enctype="multipart/form-data"name="myform1"id="myform1"><h2for="myfile1">Select files : </h2><inputtype="file"name="excelFile"id="excelFile" /><br><br><h2for="fname">First name : </h2><inputtype="text"id="fname"name="fname"><br><br><h2for="lname">Last name : </h2><inputtype="text"id="lname"name="lname"><br><br><inputtype="submit"name="btnSubmit"id="btnSubmit"value="Submit" /></form><scriptsrc="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script><scripttype="text/javascript">
$(function(){


    // āđ€āļĄื่āļ­āļŸāļ­āļĢ์āļĄāļāļēāļĢāđ€āļĢีāļĒāļāđƒāļŠ้ evnet submit āļ‚้āļ­āļĄูāļĨ        
    $("#excelFile").on("change",function(e){
        e.preventDefault(); // āļ›ิāļ”āļāļēāļĢāđƒāļŠ้āļ‡āļēāļ™ submit āļ›āļāļ•ิ āđ€āļžื่āļ­āđƒāļŠ้āļ‡āļēāļ™āļœ่āļēāļ™ ajax// āđ€āļ•āļĢีāļĒāļĄāļ‚้āļ­āļĄูāļĨ form āļŠāļģāļŦāļĢัāļšāļŠ่āļ‡āļ”้āļ§āļĒ  FormData Objectvar formData = newFormData($("#myform1")[0]);

        // āļŠ่āļ‡āļ„่āļēāđāļšāļš POST āđ„āļ›āļĒัāļ‡āđ„āļŸāļĨ์ read_excel.php āļĢูāļ›āđāļšāļš ajax āđāļšāļšāđ€āļ•็āļĄ
        $.ajax({
            url: 'read_excel.php',
            type: 'POST',
            data: formData,
            /*async: false,*/cache: false,
            contentType: false,
            processData: false
        }).done(function(data){
                console.log(data);  // āļ—āļ”āļŠāļ­āļšāđāļŠāļ”āļ‡āļ„่āļē  āļ”ูāļœ่āļēāļ™āļŦāļ™้āļē console/*              āļāļēāļĢāđƒāļŠ้āļ‡āļēāļ™ console log āđ€āļžื่āļ­ debug javascript āđƒāļ™ chrome firefox āđāļĨāļ° ie 
                http://www.ninenik.com/content.php?arti_id=692 via @ninenik         */
                $("#fname").val(data.A2);
                $("#lname").val(data.B2);
        });     

    });


});
</script></body></html>
  1. read_excel.php (PHP Code)
<?php
header("Content-type:application/json; charset=UTF-8");    
header("Cache-Control: no-store, no-cache, must-revalidate");         
header("Cache-Control: post-check=0, pre-check=0", false); 
/** Error reporting */
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Asia/Bangkok');
// http://php.net/manual/en/timezones.phprequire_once("PHPExcel/Classes/PHPExcel.php");
?><?phpif(isset($_FILES['excelFile']['name']) && $_FILES['excelFile']['name']!=""){
    $tmpFile = $_FILES['excelFile']['tmp_name'];  
    $fileName = $_FILES['excelFile']['name'];  // āđ€āļ็āļšāļŠื่āļ­āđ„āļŸāļĨ์$_fileup = $_FILES['excelFile'];
    $info = pathinfo($fileName);
    $allow_file = array("csv","xls","xlsx");
/*  print_r($info);         // āļ‚้āļ­āļĄูāļĨāđ„āļŸāļĨ์   
    print_r($_fileup);*/if($fileName!="" && in_array($info['extension'],$allow_file)){
        // āļ­่āļēāļ™āđ„āļŸāļĨ์āļˆāļēāļ path temp āļŠั่āļ§āļ„āļĢāļēāļ§āļ—ี่āđ€āļĢāļēāļ­ัāļžāđ‚āļŦāļĨāļ”$objPHPExcel = PHPExcel_IOFactory::load($tmpFile);      


        // āļ”ึāļ‡āļ‚้āļ­āļĄูāļĨāļ‚āļ­āļ‡āđāļ•่āļĨāļ°āđ€āļ‹āļĨāđƒāļ™āļ•āļēāļĢāļēāļ‡āļĄāļēāđ„āļ§้āđƒāļŠ้āļ‡āļēāļ™āđƒāļ™āļĢูāļ›āđāļšāļšāļ•ัāļ§āđāļ›āļĢ array$cell_collection = $objPHPExcel->getActiveSheet()->getCellCollection();

        // āļ§āļ™āļĨูāļ›āđāļŠāļ”āļ‡āļ‚้āļ­āļĄูāļĨ$v=1;
        $json_data = array();
        foreach ($cell_collectionas$cell) {
            // āļ„่āļēāļŠāļģāļŦāļĢัāļšāļ”ูāļ§่āļēāđ€āļ›็āļ™āļ„āļ­āļĨัāļĄāļ™์āđ„āļŦāļ™ āđ€āļŠ่āļ™ A B C ....$column = $objPHPExcel->getActiveSheet()->getCell($cell)->getColumn();
            // āļ„āļģāļŠāļģāļŦāļĢัāļšāļ”ูāļ§่āļēāđ€āļ›็āļ™āđāļ–āļ§āļ—ี่āđ€āļ—่āļēāđ„āļŦāļĢ่ āđ€āļŠ่āļ™ 1 2 3 .....$row = $objPHPExcel->getActiveSheet()->getCell($cell)->getRow();
            // āļ„่āļēāļ‚āļ­āļ‡āļ‚้āļ­āļĄูāļĨāđƒāļ™āđ€āļ‹āļĨāļĨ์āļ™ั้āļ™āđ† āđ€āļŠ่āļ™ A1 B1 C1 ....$data_value = $objPHPExcel->getActiveSheet()->getCell($cell)->getValue();          

            // āđ€āļ—่āļēāļ™ี้āđ€āļĢāļēāļ็āļŠāļēāļĄāļēāļĢāļ–āđāļŠāļ”āļ‡āļ‚้āļ­āļĄูāļĨāļˆāļēāļāļāļēāļĢāļ­่āļēāļ™āđ„āļŸāļĨ์āđ„āļ”้āđāļĨ้āļ§ āđāļĨāļ°āļŠāļēāļĄāļēāļĢāļ–āļ™āļģāļ‚้āļ­āļĄูāļĨāđ€āļŦāļĨ่āļēāļ™ี้// āļ—āļģāļāļēāļĢāļšัāļ™āļ—ัāļāļĨāļ‡āļāļēāļ™āļ‚้āļ­āļĄูāļĨ āļŦāļĢืāļ­āđāļŠāļ”āļ‡āđ„āļ”้āđ€āļĨāļĒ$json_data["$column$row"] = $data_value;
//            echo $v." ----  ".$data_value."<br>";$v++;
        }       
         // āđāļ›āļĨāļ‡ array āđ€āļ›็āļ™āļĢูāļ›āđāļšāļš json string  if(isset($json_data)){  
            $json= json_encode($json_data);    
            if(isset($_GET['callback']) && $_GET['callback']!=""){    
            echo$_GET['callback']."(".$json.");";        
            }else{    
            echo$json;    
            }    
        }        
    }
} 
?>

I use PHPExcel library to download with link.

Post a Comment for "How To Parse Response Table With Phpexcel Import Excel File To Input Tag (example : Autofill Input Id="fname" To Hello And Input Id="lname" To World)?"