2014-07-26 23 views
1

我是json中的新成員。我使用php從mysql表中生成jason數據,並希望將生成的json導出爲.xls格式。使用Jquery Easyui將datagrid導出爲ex​​cel

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 = se mester' 我得到一個ReferenceError exportExcel未定義。

+0

你試過 - >'document.location ='excel/examexport.php?entry ='+ $('#semester').val()+'&batch =' + $('#batch')。val()+'&semester ='+ $('#entry')。val()' – Sean

+0

@Sean,我已經試過了,但它不起作用。 –

回答

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。他們有很好的文檔。

+0

非常感謝。像魅力一樣工作。它簡短而簡單。我很喜歡。 –

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

你可以使用簡單的jQuery代碼。你應該在你的文件中添加jQuery庫。

+0

您能否詳細說明您的答案,並增加關於您提供的解決方案的更多描述? – abarisone