2010-02-03 140 views
19

有人知道將數據從jqgrid導出爲ex​​cel的方法嗎?php + jqgrid +導出爲ex​​cel

我想使用這個jqgrid做一個報告,我覺得它很可怕。但我需要以某種方式保存或打印此報告,因爲需要保存信息。 有人知道任何方式?

回答

11

這是我的做法,只是將此代碼添加到您的JS/HTML文件

$("#list").jqGrid('navGrid', '#pager',{view:true, del:false, add:false, edit:false, excel:true}) 
       .navButtonAdd('#pager',{ 
           caption:"Export to Excel", 
           buttonicon:"ui-icon-save", 
           onClickButton: function(){ 
            exportExcel(); 
           }, 
           position:"last" 
          }); 

     function exportExcel() 
     { 
      var mya=new Array(); 
      mya=$("#list").getDataIDs(); // Get All IDs 
      var data=$("#list").getRowData(mya[0]);  // Get First row to get the labels 
      var colNames=new Array(); 
      var ii=0; 
      for (var i in data){colNames[ii++]=i;} // capture col names 
      var html=""; 
      for(i=0;i<mya.length;i++) 
       { 
       data=$("#list").getRowData(mya[i]); // get each row 
       for(j=0;j<colNames.length;j++) 
        { 
        html=html+data[colNames[j]]+"\t"; // output each column as tab delimited 
        } 
       html=html+"\n"; // output each row with end of line 

       } 
      html=html+"\n"; // end of line at the end 
      document.forms[0].csvBuffer.value=html; 
      document.forms[0].method='POST'; 
      document.forms[0].action='csvExport.php'; // send it to server which will open this contents in excel file 
      document.forms[0].target='_blank'; 
      document.forms[0].submit(); 
     } 

PHP腳本

header('Content-type: application/vnd.ms-excel'); 
header("Content-Disposition: attachment; filename=file.xls"); 
header("Pragma: no-cache"); 

$buffer = $_POST['csvBuffer']; 

try{ 
    echo $buffer; 
}catch(Exception $e){ 

} 
+1

感謝分享您的代碼費利克斯·格雷羅。我有一個問題:當我運行你的代碼時,我得到這個錯誤:document.forms [0] .csvBuffer是未定義的,你能幫我解決這個問題嗎? – 2011-02-22 20:05:57

+0

此代碼確實看起來只考慮可見行,您是否有任何關於如何獲取完整列表的提示(即不僅是當前頁面中的行)? – Don 2011-03-28 14:49:57

+0

這是否從後端獲取數據?我想不是.. – 2012-10-11 02:21:05

0

創建一個表單,並命名爲 「csvBuffer」 的隱藏要素。該元素由函數設置。 我不得不行

html = html+"\n" 

改變

​​

,以便正確地逃避它。

3

非常好的問題,我也在撓撓我的頭腦。 我是通過選擇Felix的建議做出來的,讓我通過在你的 html主體中添加以下幾行來完成它。

<form method="post" action="csvExport.php"> 
    <input type="hidden" name="csvBuffer" id="csvBuffer" value="" /> 
</form> 

我唯一的問題是導出Excel文件犯規包含的jqGrid我的列名,也就是有沒有辦法導出到Excel文件時要排除特定列或多列?

謝謝〜

1

我解決你的問題。而現在亞姆能夠導出excel數據與列名請參考我的代碼。

function exportExcel() 
    { 
     var mya=new Array(); 
     mya=$("#tblnoupdate").getDataIDs(); // Get All IDs 
     var data=$("#tblnoupdate").getRowData(mya[0]);  // Get First row to get the labels 
     var colNames=new Array(); 
     var ii=0; 
     for (var i in data){colNames[ii++]=i;} // capture col names 
     var html=""; 
      for(k=0;k<colNames.length;k++) 
      { 
      html=html+colNames[k]+"\t";  // output each Column as tab delimited 
      } 
      html=html+"\n";     // Output header with end of line 
     for(i=0;i<mya.length;i++) 
      { 
      data=$("#tblnoupdate").getRowData(mya[i]); // get each row 
      for(j=0;j<colNames.length;j++) 
       { 
      html=html+data[colNames[j]]+"\t"; // output each Row as tab delimited 
       } 
      html=html+"\n"; // output each row with end of line 

      } 
     html=html+"\n"; // end of line at the end 
     document.forms[0].csvBuffer.value=html; 
     document.forms[0].method='POST'; 
     document.forms[0].action='<?php echo $baseurl;?>csvexport.php'; // send it to server which will open this contents in excel file 
     document.forms[0].target='_blank'; 
     document.forms[0].submit(); 
    } 

如果您遇到任何問題,請讓我知道。

2

很棒的功能!
我做了更改。

 
function exportExcel($id){ 
    var keys=[], ii=0, rows=""; 
    var ids=$id.getDataIDs(); // Get All IDs 
    var row=$id.getRowData(ids[0]);  // Get First row to get the labels 
    for (var k in row) { 
    keys[ii++]=k; // capture col names 
    rows=rows+k+"\t";  // output each Column as tab delimited 
    } 
    rows=rows+"\n"; // Output header with end of line 
    for(i=0;i<ids.length;i++) { 
    row=$id.getRowData(ids[i]); // get each row 
    for(j=0;j<keys.length;j++) rows=rows+row[keys[j]]+"\t"; // output each Row as tab delimited 
    rows=rows+"\n"; // output each row with end of line 
    } 
    rows=rows+"\n"; // end of line at the end 
    var form = "<form name='csvexportform' action='"+php_path+"csvexport.php' method='post'>"; 
    form = form + "<input type='hidden' name='csvBuffer' value='"+rows+"'>"; 
    form = form + "</form><script>document.csvexportform.submit();</sc"+"ript>"; 
    OpenWindow=window.open('', ''); 
    OpenWindow.document.write(form); 
    OpenWindow.document.close(); 
} 

function gridcsvexport(id) { 
    $('#'+id).jqGrid('navButtonAdd','#'+id+'_pager',{ 
    caption:'', 
    title:'export', 
    buttonicon:'ui-icon-newwin', 
    position:'last', 
    onClickButton:function(){ 
     exportExcel($(this)); 
    } 
    }); 
} 
1

這裏是一個聰明的解決方案來保存jqGrid數據爲Excel工作表,而不調用php腳本:(你只需要調用這個函數與GridID和可選Filename

var createExcelFromGrid = function(gridID,filename) { 
    var grid = $('#' + gridID); 
    var rowIDList = grid.getDataIDs(); 
    var row = grid.getRowData(rowIDList[0]); 
    var colNames = []; 
    var i = 0; 
    for(var cName in row) { 
     colNames[i++] = cName; // Capture Column Names 
    } 
    var html = ""; 
    for(var j=0;j<rowIDList.length;j++) { 
     row = grid.getRowData(rowIDList[j]); // Get Each Row 
     for(var i = 0 ; i<colNames.length ; i++) { 
      html += row[colNames[i]] + ';'; // Create a CSV delimited with ; 
     } 
     html += '\n'; 
    } 
    html += '\n'; 

    var a   = document.createElement('a'); 
    a.id = 'ExcelDL'; 
    a.href  = 'data:application/vnd.ms-excel,' + html; 
    a.download = filename ? filename + ".xls" : 'DataList.xls'; 
    document.body.appendChild(a); 
    a.click(); // Downloads the excel document 
    document.getElementById('ExcelDL').remove(); 
} 

我們首先創建一個CSV字符串分隔。然後創建一個anchor標籤,並帶有某些屬性。最後在a上調用click來下載文件。

你可以看看幾個Excel的MIME類型:MIME Type List