之前有介绍过如何实现easyui里datagrid内容的打印,今天给大家介绍下如何实现datagrid内容导出为excel文件。以下为代码实现:
export.js
function changetotable(printdatagrid) {
var tablestring = '<table cellspacing="0" class="pb">';
var frozencolumns = printdatagrid.datagrid("options").frozencolumns; // 得到frozencolumns对象
var columns = printdatagrid.datagrid("options").columns; // 得到columns对象
var namelist = new array();
// 载入title
if (typeof columns != 'undefined' && columns != '') {
$(columns).each(function (index) {
tablestring += '\n<tr>';
if (typeof frozencolumns != 'undefined' && typeof frozencolumns[index] != 'undefined') {
for (var i = 0; i < frozencolumns[index].length; ++i) {
if (!frozencolumns[index][i].hidden) {
tablestring += '\n<th width="' + frozencolumns[index][i].width + '"';
if (typeof frozencolumns[index][i].rowspan != 'undefined' && frozencolumns[index][i].rowspan > 1) {
tablestring += ' rowspan="' + frozencolumns[index][i].rowspan + '"';
}
if (typeof frozencolumns[index][i].colspan != 'undefined' && frozencolumns[index][i].colspan > 1) {
tablestring += ' colspan="' + frozencolumns[index][i].colspan + '"';
}
if (typeof frozencolumns[index][i].field != 'undefined' && frozencolumns[index][i].field != '') {
namelist.push(frozencolumns[index][i]);
}
tablestring += '>' + frozencolumns[0][i].title + '</th>';
}
}
}
for (var i = 0; i < columns[index].length; ++i) {
if (!columns[index][i].hidden) {
tablestring += '\n<th width="' + columns[index][i].width + '"';
if (typeof columns[index][i].rowspan != 'undefined' && columns[index][i].rowspan > 1) {
tablestring += ' rowspan="' + columns[index][i].rowspan + '"';
}
if (typeof columns[index][i].colspan != 'undefined' && columns[index][i].colspan > 1) {
tablestring += ' colspan="' + columns[index][i].colspan + '"';
}
if (typeof columns[index][i].field != 'undefined' && columns[index][i].field != '') {
namelist.push(columns[index][i]);
}
tablestring += '>' + columns[index][i].title + '</th>';
}
}
tablestring += '\n</tr>';
});
}
// 载入内容
var rows = printdatagrid.datagrid("getrows"); // 这段代码是获取当前页的所有行
for (var i = 0; i < rows.length; ++i) {
tablestring += '\n<tr>';
for (var j = 0; j < namelist.length; ++j) {
var e = namelist[j].field.lastindexof('_0');
tablestring += '\n<td';
if (namelist[j].align != 'undefined' && namelist[j].align != '') {
tablestring += ' style="text-align:' + namelist[j].align + ';"';
}
tablestring += '>';
if (e + 2 == namelist[j].field.length) {
tablestring += rows[i][namelist[j].field.substring(0, e)];
}
else
tablestring += rows[i][namelist[j].field];
tablestring += '</td>';
}
tablestring += '\n</tr>';
}
tablestring += '\n</table>';
return tablestring;
}
function export(strxlsname, exportgrid) {
var f = $('<form action="/export.aspx" method="post" id="fm1"></form>');
var i = $('<input type="hidden" id="txtcontent" name="txtcontent" />');
var l = $('<input type="hidden" id="txtname" name="txtname" />');
i.val(changetotable(exportgrid));
i.appendto(f);
l.val(strxlsname);
l.appendto(f);
f.appendto(document.body).submit();
document.body.removechild(f);
}
export.aspx
protected void page_load(object sender, eventargs e)
{
response.clear();
response.buffer = true;
response.charset = "utf-8";
response.contentencoding = system.text.encoding.utf8;
response.appendheader("content-disposition", "attachment;filename=\"" + httputility.htmlencode(request["txtname"]datetime.now.tostring("yyyymmdd")) + ".xls\"");
response.contenttype = "application/ms-excel";
response.write("<html>\n<head>\n");
response.write("<style type=\"text/css\">\n.pb{font-size:13px;border-collapse:collapse;} "+
"\n.pb th{font-weight:bold;text-align:center;border:0.5pt solid windowtext;padding:2px;} " +
"\n.pb td{border:0.5pt solid windowtext;padding:2px;}\n</style>\n</head>\n");
response.write("<body>\n" + request["txtcontent"] + "\n</body>\n</html>");
response.flush();
response.end();
}
其中export.aspx为了防止前台页面内容干扰,前台页面只留:
<%@ page language="c#" autoeventwireup="true" codebehind="export.aspx.cs" inherits="newland.webui.export" validaterequest="false" %>
这句话,其他的全部删除。
调用方法:
<a href="javascript:void(0);" onclick="export('导出excel', $('#grid'));">导出</a>
以上就是小编为大家带来的实现easyui的datagrid导出为excel的示例代码全部内容了
