1.第一步获取前台DataGrid中的数据
var rows = $(‘#tb).datagrid("getRows");
if (rows.length == 0) {
$("#ShowMsg").html("没有数据可供导出!");
return;
}
var columns = new Array();
var fields = $(‘#tb).datagrid(‘getColumnFields‘);
for (var i = 0; i < fields.length; i++) {
var opts = $(‘#tb).datagrid(‘getColumnOption‘, fields[i]);
var column = new Object();
column.field = opts.field;
column.title = opts.title;
columns.push(column);
}
var excelWorkSheet = new Object();
excelWorkSheet.rows = rows;
excelWorkSheet.columns = columns;
excelWorkSheet.sheetName = "名字";
2.发布Post请求到后台
$.post("XXXX.aspx", { IsAction: ‘Method‘, ActType: ‘exportXls‘,
ExcelWorkSheet: JSON.stringify(excelWorkSheet) //将数据转化为Json字符串
},
function (data) {
成功后操作
}
);
3.在后台将Json串转化为Datatable后写入到Excel模版中;