1.抽取DataGrid列表数据
function ExportNormal(strXlsName, exportGrid, postUrl, hiddenColumns) { /// <summary> /// 提交DataGrid数据 /// </summary> var f = $('<form action="' + postUrl + '?ac=normal" method="post" id="fmExport1"></form>'); var i = $('<input type="hidden" id="Content" name="Content" />'); var l = $('<input type="hidden" id="Name" name="Name" />'); i.val(ChangeToTableNormal(exportGrid, hiddenColumns)); i.appendTo(f); l.val(strXlsName); l.appendTo(f); f.appendTo(document.body).submit(); $("#fmExport1").remove(); } function ChangeToTableNormal(printDatagrid, hiddenColumns) { /// <summary> /// 提交DataGrid数据 /// </summary> var frozenColumns = printDatagrid.datagrid("options").frozenColumns; // 得到frozenColumns对象 var columns = printDatagrid.datagrid("options").columns; // 得到frozenColumns对象 columns = (frozenColumns[0] || []).concat(columns[0] || []).concat(columns[1] || []); var rows = printDatagrid.datagrid("getRows"); var data = new Array(); var tmp = new Array(); //列名 $.map(columns, function (a) { if (!a.hidden && a.field) { tmp.push(a.title); } }); data.push(tmp); $.map(rows, function (r) { tmp = new Array(); $.map(columns, function (c) { if (!c.hidden && c.field) { if (c.formatter) { var v = c.formatter.call(this, r[c.field], r) || ""; v = $(v).text() if (v.toString().indexOf("<a") >= 0) tmp.push(fixGetValue(r[c.field])); else tmp.push(v); } else { tmp.push(fixGetValue(r[c.field])); } } }); data.push(tmp); }); return JSON.stringify(data); } function fixGetValue(obj) { return obj ? obj : ""; }
2.把抽取的数据提交到后台,生成excel
ExportNormal("人员名单导出", $('#flexigridData'),"/CreateExcel.aspx");
//提交的数据格式 Content:[["姓名","性别","证件号","手机号","证件类型"],["小强","男","333333333333333","1800000000","身份证"]]