2010-02-03 127 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