使用Jquery Easyui将datagrid导出为ex​​cel

时间:2014-07-26 15:25:18

标签: php excel datagrid jquery-easyui

我是json的新人。我使用php从mysql表生成jason数据,并希望将生成的json导出为.xls格式。

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

我的php生成了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"}]}

导出到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>

我想发送类似document.location =&#39; excel / examexport.php?entry = entry&amp; batch = batch&amp; semester = semester&#39; 我得到了一个ReferenceError,没有定义exportExcel。

2 个答案:

答案 0 :(得分:1)

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

你可以使用简单的jquery代码。你应该在你的文件上添加jquery库。

答案 1 :(得分:0)

在examexport.php中,将$ _POST更改为$ _GET并使用以下函数:

<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>

最后一句话,请完成http://www.jeasyui.com/documentation/index.php。他们有很好的文档。