如何在不调用服务器端的情况下从JqGrid导出数据到excel

how to export from JqGrid data to excel without a server side call?

本文关键字:JqGrid 数据 excel 情况下 调用 服务器端      更新时间:2023-09-26

互联网上的所有文章都指出,需要一个服务器端调用才能将jqgrid导出到excel。我正在寻找一种机制,在那里我可以导出一次加载网格excel而不必打电话。如何实现呢?

您可以捕获Export按钮的单击事件,然后通过下面的javascript遍历jqgrid:

$(':button[id=ExportExcel]').click(function () {
for (iRow = 0; iRow < cRows; iRow++) {
    row = rows[iRow];
    if ($(row).hasClass("jqgrow")) {
        cellsOfRow = row.cells;
            $(cellsOfRow[iCol]).text()
               // construct your html table here and then, combining code from below 
            }
       }:

然后使用下面类似的代码填充准备导出的表,但是使用jqgrid数据作为源,而不是下面创建的html表。

function fnExcelReport()
{
    var tab_text="<table><tr>";
    var textRange;
    tab = document.getElementById('TableData'); // id of actual table on your page
    for(j = 0 ; j < tab.rows.length ; j++) 
    {   
        tab_text=tab_text+tab.rows[j].innerHTML;
        tab_text=tab_text+"</tr><tr>";
    }
    tab_text = tab_text+"</tr></table>";
    var txt = document.getElementById('txtArea1').contentWindow;
    txt.document.open("txt/html","replace");
    txt.document.write(tab_text);
    txt.document.close();
    txt.focus();
    tb = txt.document.execCommand("SaveAs",true,"Say Thanks to Sumit.xls");
    return (tb);
    }