Export datagrid to excel using Jquery Easyui

只谈情不闲聊 提交于 2019-12-18 11:17:06

问题


I am new in json. I generated jason data from mysql table using php and want to export the generated json to .xls format.

examexport.php

<?php
    include 'conn.php';

    $page = isset($_POST['page']) ? intval($_POST['page']) : 1;
    $rows = isset($_POST['rows']) ? intval($_POST['rows']) : 10;
    $semester = isset($_POST['semester']) ? 
    mysql_real_escape_string($_POST['semester']) : '';
    $entry = isset($_POST['entry']) ? mysql_real_escape_string($_POST['entry']) : '';
    $batch = isset($_POST['batch']) ? mysql_real_escape_string($_POST['batch']) : '';

    //phpexcel things go here
?>

My php generated json:

 {"total":"6","rows":
 [{"id":"2","regd":"25","name":"Lalhmangaihsangi","class":"BA",
"rollno":"3","univ_roll":"UNVI573","univ_no":"MZU876","core":"Education",
"semester":"First","batch":"2014","subject":"Education",
"entry":"Second Internal Test","date":"2014-07-23",
"score":"55","fm":"100","remark":"She is guarded"}]}

Code for export to excel:

<input type="button" onclick="exportExcel()" value="Export to Excel " />
<script>                
  function exportExcel(){
    $('#dg').datagrid('load',{ //load data by semester/batch/entry
    semester: $('#semester').val(),
    batch: $('#batch').val(),
    entry: $('#entry').val(),
    document.location='excel/examexport.php'// How do I include entry/batch/ here?
 });
 }
</script>

I want to send something like document.location='excel/examexport.php?entry=entry&batch=batch&semester=semester' I got a ReferenceError exportExcel is not defined.


回答1:


In your examexport.php, change $_POST to $_GET and use the following function:

<input type="button" onclick="exportExcel()" value="Export to Excel " />
<script>
    function exportExcel() {
        var row=$('#dg').datagrid('getData');//to get the loaded data
        if (row) {
            url = "excel/examexport.php?entry="+$('#entry').val()+"&
                   semester="+$('#semester').val()+"&batch="+$('#batch').val();
            window.open(url);
            }
    }
 </script>

For final word, please go through http://www.jeasyui.com/documentation/index.php. they have good documentation.




回答2:


$("#btnExport").click(function(e) {
window.open('data:application/vnd.ms-excel,' + encodeURIComponent($('#dg').html()));
e.preventDefault(); });

you can use just simple jquery code. you should add jquery library on your file.​



来源:https://stackoverflow.com/questions/24972547/export-datagrid-to-excel-using-jquery-easyui

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!