【问题标题】:Export datagrid to excel using Jquery Easyui使用 Jquery Easyui 将数据网格导出到 Excel
【发布时间】:2014-07-26 15:25:17
【问题描述】:

我是 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='excel/examexport.php?entry=entry&batch=batch&semester=semester' 的内容 我得到一个 ReferenceError exportExcel is not defined。

【问题讨论】:

  • 你试过了吗-> document.location='excel/examexport.php?entry='+$('#semester').val()+'&amp;batch='+$('#batch').val()+'&amp;semester='+$('#entry').val()
  • @Sean,我已经试过了,但是不行。

标签: php excel datagrid jquery-easyui


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

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

【讨论】:

  • 能否请您详细说明您的答案,添加更多关于您提供的解决方案的描述?
【解决方案2】:

在您的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。他们有很好的文档。

【讨论】:

  • 非常感谢。像魅力一样工作。它简短而简单。我真的很喜欢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-04-18
  • 2023-03-30
相关资源
最近更新 更多