javascript - 如何在没有服务器端调用的情况下从 JqGrid 数据导出到 excel?

标签 javascript jquery jqgrid

网上所有文章都说jqgrid导出到excel需要服务器端调用。我正在寻找一种机制,无需调用即可将一次加载的网格导出到 excel。我怎样才能做到这一点?

最佳答案

您可以捕获导出按钮的点击事件,然后通过下面的 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);
    }

关于javascript - 如何在没有服务器端调用的情况下从 JqGrid 数据导出到 excel?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25293408/

相关文章:

javascript - Jquery - 使用无序列表项显示已完成步骤的多步骤表单进度条

jquery - 在jqgrid中,如何让编辑表单在提交后消失

javascript - jqGrid 错误 - "Element is not a table"

javascript - 在 Angular Directive(指令)中完成 ng-repeat

javascript - 批量转换 IP 地址以查找位置

javascript - 无法读取未定义的属性 'split'

javascript - 将鼠标悬停在多个 div 之一上并延迟,然后下拉子 div

jqGrid 在提交时更改搜索过滤器

javascript - `new Date()` 解析依赖于格式

javascript - 如何在 Angular JS 中将特定的 div 文本复制到剪贴板