Search code examples
phphtmlexcelphpexcelhtml-input

How to fix code to manual select excel column at input id="selectcell1" and "selectcell2" (Ex : Select A2 in "selectcell1" to C2 in "selectcell2")?


How to fix code to support manual select excel column at input id="selectcell1" and input id="selectcell2" (Example : Select A2 in input id="selectcell1" to select C2 in input id="selectcell2")?

I try to coding with sample code and file at the bottom (use code with credit at link).

  1. excelimport2.xlsx (Excel file)

Download Link

  1. excelimport.php (PHP Code)
<!DOCTYPE html>  
<html>  
<head>  
    <meta charset="UTF-8">  
    <title>Document</title>
    <style>
    h2,h4 {display: inline;}
    </style>
</head>

<body>

<form action="" method="post" enctype="multipart/form-data" name="myform1" id="myform1">

    <details>
        <summary>Input Excel cell to select.</summary>
        <br><h4 for="selectcell1">Excel cell to select at first : </h4><input type="text" id="selectcell1" name="selectcell1"><br><br>
        <h4 for="selectcell2">Excel cell to select at final : </h4><input type="text" id="selectcell2" name="selectcell2">
    </details><br>

    <h2 for="myfile1">Select files : </h2><input type="file" name="excelFile" id="excelFile" /><br><br>
    <h2 for="fname">First name : </h2><input type="text" id="fname" name="fname"><br><br>
    <h2 for="lname">Middle name : </h2><input type="text" id="lname" name="lname"><br><br>
    <h2 for="lname">Last name : </h2><input type="text" id="mname" name="mname"><br><br>
  <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
</form>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>     
<script type="text/javascript">
$(function(){


    // เมื่อฟอร์มการเรียกใช้ evnet submit ข้อมูล        
    $("#excelFile").on("change",function(e){
        e.preventDefault(); // ปิดการใช้งาน submit ปกติ เพื่อใช้งานผ่าน ajax

        // เตรียมข้อมูล form สำหรับส่งด้วย  FormData Object
       var formData = new FormData($("#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         */

                $('#selectcell1').val($('#selectcell1').val().toUpperCase());
                $('#selectcell2').val($('#selectcell2').val().toUpperCase());

                if($("#selectcell1").val() != ""){
                $("#fname").val(eval("data." + $("#selectcell1").val()));
                }
                if($("#selectcell2").val() != ""){
                $("#lname").val(eval("data." + $("#selectcell2").val()));
                }
                if($("#selectcell3").val() != ""){
                $("#mname").val(eval("data." + $("#selectcell3").val()));
                }
        });     
    });   
});
</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.php
require_once("PHPExcel/Classes/PHPExcel.php");
?>
<?php 
if(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_collection as $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.

But I can't to fix code to support manual select excel column at input id="selectcell1" and input id="selectcell2" (Example : Select A2 in input id="selectcell1" to select C2 in input id="selectcell2").


Solution

  • Good news : I have answer to support manual select horizontal excel cell at input id="selectcell1" and input id="selectcell2" (Example : Select A2 in input id="selectcell1" to select C2 in input id="selectcell2") with my full source code.

    I have answer credit with link.

    1. excelimport3.xlsx (Excel file)

    Download Link

    1. excelimport.php (PHP Code)
    <!DOCTYPE html>  
    <html>  
    <head>  
        <meta charset="UTF-8">  
        <title>Document</title>
        <style>
        h2,h4 {display: inline;}
        </style>
    </head>
    
    <body>
    
    <form action="" method="post" enctype="multipart/form-data" name="myform1" id="myform1">
    
        <details>
            <summary>Input Excel cell to select.</summary>
            <br><h4 for="selectcell1">Excel cell to select at first : </h4><input type="text" id="selectcell1" name="selectcell1"><br><br>
            <h4 for="selectcell2">Excel cell to select at final : </h4><input type="text" id="selectcell2" name="selectcell2"><br><br>
            <h4 for="selectcell1">Excel cell to select at first 2 : </h4><input type="text" id="selectcell3" name="selectcell3"><br><br>
            <h4 for="selectcell2">Excel cell to select at final 2 : </h4><input type="text" id="selectcell4" name="selectcell4">
        </details><br>
    
        <h2 for="myfile1">Select files : </h2><input type="file" name="excelFile" id="excelFile" /><br><br>
        <h2 for="fname">First name : </h2><input type="text" class="my-cssclass" id="fname" name="fname"><br><br>
        <h2 for="lname">Middle name : </h2><input type="text" class="my-cssclass" id="lname" name="lname"><br><br>
        <h2 for="lname">Last name : </h2><input type="text" class="my-cssclass" id="mname" name="mname"><br><br>
        <h2 for="fname">First name 2 : </h2><input type="text" class="my-cssclass2" id="fname2" name="fname2"><br><br>
        <h2 for="lname">Middle name 2 : </h2><input type="text" class="my-cssclass2" id="lname2" name="lname2"><br><br>
        <h2 for="lname">Last name 2 : </h2><input type="text" class="my-cssclass2" id="mname2" name="mname2"><br><br>
      <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
    </form>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>     
    <script type="text/javascript">
    $(function(){
    
    
        // เมื่อฟอร์มการเรียกใช้ evnet submit ข้อมูล        
        $("#excelFile").on("change",function(e){
            e.preventDefault(); // ปิดการใช้งาน submit ปกติ เพื่อใช้งานผ่าน ajax
    
            // เตรียมข้อมูล form สำหรับส่งด้วย  FormData Object
           var formData = new FormData($("#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         */
    
            $('#selectcell1').val($('#selectcell1').val().toUpperCase());
            $('#selectcell2').val($('#selectcell2').val().toUpperCase());
            $('#selectcell3').val($('#selectcell3').val().toUpperCase());
            $('#selectcell4').val($('#selectcell4').val().toUpperCase());
    
            // จำลองค่าจาก input element เริ่มต้น และสิ้นสุด
            var cellStart = $("#selectcell1").val();
            var cellEnd = $("#selectcell2").val();
    
            var cellStart2 = $("#selectcell3").val();
            var cellEnd2 = $("#selectcell4").val();
    
            // ใช้คำสั่ง .replace() ตัดตัวเลขจากข้อความ เหลือไว้แค่ตัวอักษร
            var charStart = cellStart.replace(/[0-9]/,'');  // A
            var charEnd = cellEnd.replace(/[0-9]/,'');  // C
            // ใช้คำสั่ง .replace() ตัดตัวอักษรจากข้อความ แล้วแปลงข้อความที่เหลือเป็นตัวเลขด้วย parseInt()
            var rowNum = parseInt(cellStart.replace(/[A-Z]/,'')); // 2
    
            // ใช้คำสั่ง .replace() ตัดตัวเลขจากข้อความ เหลือไว้แค่ตัวอักษร
            var charStart2 = cellStart2.replace(/[0-9]/,'');  // A
            var charEnd2 = cellEnd2.replace(/[0-9]/,'');  // C
            // ใช้คำสั่ง .replace() ตัดตัวอักษรจากข้อความ แล้วแปลงข้อความที่เหลือเป็นตัวเลขด้วย parseInt()
            var rowNum2 = parseInt(cellStart2.replace(/[A-Z]/,'')); // 2
    
            // ฟังก์ชั่นสร้าง array range ตัวอย่าง เช่น range("A","C")  จะได้ ["A","B","C"];
            function range(start,stop) {
                var result=[];
                for (var idx=start.charCodeAt(0),end=stop.charCodeAt(0); idx <=end; ++idx){
                result.push(String.fromCharCode(idx));
            }
            return result;
            };
    
            // วนลูป Array ตัวอักษร เพื่อใช้ อ้างอิงข้อมูล
            range(charStart, charEnd).map((value, key) => {
                console.log("data." + value + rowNum);
                var dataValue = eval("data." + value + rowNum);
                $(".my-cssclass").eq(key).val(dataValue);
            });
    
            // วนลูป Array ตัวอักษร เพื่อใช้ อ้างอิงข้อมูล
            range(charStart2, charEnd2).map((value2, key2) => {
                console.log("data." + value2 + rowNum2);
                var dataValue2 = eval("data." + value2 + rowNum2);
                $(".my-cssclass2").eq(key2).val(dataValue2);
            });
    
            });     
        });   
    });
    </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.php
    require_once("PHPExcel/Classes/PHPExcel.php");
    ?>
    <?php 
    if(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_collection as $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.

    Extra : I have answer to support manual select vertical excel cell at input id="selectcell1" and input id="selectcell2" (Example : Select A2 in input id="selectcell1" to select A4 in input id="selectcell2") and support number more than 10 with my full source code.

    I have answer credit with link.

    1. excelimport3.xlsx (Excel file)

    Download Link

    1. excelimport.php (PHP Code)
    <!DOCTYPE html>  
    <html>  
    <head>  
        <meta charset="UTF-8">  
        <title>Document</title>
        <style>
        h2,h4 {display: inline;}
        </style>
    </head>
    
    <body>
    
    <form action="" method="post" enctype="multipart/form-data" name="myform1" id="myform1">
    
        <details>
            <summary>Input Excel cell to select.</summary>
            <br><h4 for="selectcell1">Excel cell to select at first : </h4><input type="text" id="selectcell1" name="selectcell1"><br><br>
            <h4 for="selectcell2">Excel cell to select at final : </h4><input type="text" id="selectcell2" name="selectcell2"><br><br>
            <h4 for="selectcell1">Excel cell to select at first 2 : </h4><input type="text" id="selectcell3" name="selectcell3"><br><br>
            <h4 for="selectcell2">Excel cell to select at final 2 : </h4><input type="text" id="selectcell4" name="selectcell4">
        </details><br>
    
        <h2 for="myfile1">Select files : </h2><input type="file" name="excelFile" id="excelFile" /><br><br>
        <h2 for="fname">First name : </h2><input type="text" class="my-cssclass" id="fname" name="fname"><br><br>
        <h2 for="lname">Middle name : </h2><input type="text" class="my-cssclass" id="lname" name="lname"><br><br>
        <h2 for="lname">Last name : </h2><input type="text" class="my-cssclass" id="mname" name="mname"><br><br>
        <h2 for="fname">First name 2 : </h2><input type="text" class="my-cssclass2" id="fname2" name="fname2"><br><br>
        <h2 for="lname">Middle name 2 : </h2><input type="text" class="my-cssclass2" id="lname2" name="lname2"><br><br>
        <h2 for="lname">Last name 2 : </h2><input type="text" class="my-cssclass2" id="mname2" name="mname2"><br><br>
      <input type="submit" name="btnSubmit" id="btnSubmit" value="Submit" />
    </form>
    
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>     
    <script type="text/javascript">
    $(function(){
    
    
        // เมื่อฟอร์มการเรียกใช้ evnet submit ข้อมูล        
        $("#excelFile").on("change",function(e){
            e.preventDefault(); // ปิดการใช้งาน submit ปกติ เพื่อใช้งานผ่าน ajax
    
            // เตรียมข้อมูล form สำหรับส่งด้วย  FormData Object
           var formData = new FormData($("#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  */
    
            $('#selectcell1').val($('#selectcell1').val().toUpperCase());
            $('#selectcell2').val($('#selectcell2').val().toUpperCase());
            $('#selectcell3').val($('#selectcell3').val().toUpperCase());
            $('#selectcell4').val($('#selectcell4').val().toUpperCase());
    
            // จำลองค่าจาก input element เริ่มต้น และสิ้นสุด แบบแนวตั้งครับ
            var cellStart = $("#selectcell1").val();
            var cellEnd = $("#selectcell2").val();
    
            var cellStart2 = $("#selectcell3").val();
            var cellEnd2 = $("#selectcell4").val();
    
            // ใช้คำสั่ง .replace() ตัดตัวอักษรจากข้อความ แล้วแปลงข้อความที่เหลือเป็นตัวเลขด้วย parseInt() 
            var minV = parseInt(cellStart.replace(/[A-Z]/,'')); // ได้ค่าเริ่มต้นตัวเลข สมมติ "A2" จะได้เป็น 2
            var maxV = parseInt(cellEnd.replace(/[A-Z]/,''));   // ได้ค่าสิ้นสุดตัวเลข สมมติ "A5" จะได้เป็น 5
    
            // ใช้คำสั่ง .replace() ตัดตัวเลขจากข้อความ เหลือไว้แค่ตัวอักษร
            var colChar = cellEnd.replace(/[0-9]+/,''); // สมมติ "A5" จะได้เป็น A
    
            // ใช้คำสั่ง .replace() ตัดตัวอักษรจากข้อความ แล้วแปลงข้อความที่เหลือเป็นตัวเลขด้วย parseInt()
            var minV2 = parseInt(cellStart2.replace(/[A-Z]/,'')); // ได้ค่าเริ่มต้นตัวเลข สมมติ "A2" จะได้เป็น 2
            var maxV2 = parseInt(cellEnd2.replace(/[A-Z]/,''));   // ได้ค่าสิ้นสุดตัวเลข สมมติ "A5" จะได้เป็น 5
    
            // ใช้คำสั่ง .replace() ตัดตัวเลขจากข้อความ เหลือไว้แค่ตัวอักษร
            var colChar2 = cellEnd2.replace(/[0-9]+/,''); // สมมติ "A5" จะได้เป็น A
    
            var idx = 0;
            var idx2 = 0;
    
            // วนลูปเพื่ออ้างอิงตำแหน่ง property ของ object ตามช่วงข้อมูล
            for ( var c = minV; c <= maxV; c++){
                console.log("data." + colChar + c); // จะได้ค่า เป็น "data.A2" , "data.A3"... "data.A5");
                var dataValue = eval("data." + colChar + c);
                $(".my-cssclass").eq(idx).val(dataValue);
                idx++;
            } 
    
            // วนลูปเพื่ออ้างอิงตำแหน่ง property ของ object ตามช่วงข้อมูล
            for ( var c2 = minV2; c2 <= maxV2; c2++){
                console.log("data." + colChar2 + c2); // จะได้ค่า เป็น "data.A2" , "data.A3"... "data.A5");
                var dataValue2 = eval("data." + colChar2 + c2);
                $(".my-cssclass2").eq(idx2).val(dataValue2);
                idx2++;
            }
    
            });     
        });   
    });
    </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.php
    require_once("PHPExcel/Classes/PHPExcel.php");
    ?>
    <?php 
    if(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_collection as $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.