网上所有文章都说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/