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