当前位置: 移动技术网 > IT编程>开发语言>.net > .net中将DataTable导出到word、Excel、txt、htm的方法

.net中将DataTable导出到word、Excel、txt、htm的方法

2019年05月10日  | 移动技术网IT编程  | 我要评论

ha.122.gow.gn,热河杆子帮原型,工业区位论

dt:datatable

strfile:filename

strext:type


private void gridexport(datatable dt, string strfile, string strext)
{
string strapptype = "";
switch (strext)
{
case "xls":
strapptype = "application/ms-excel";
break;
case "doc":
strapptype = "application/ms-word";
break;
case "txt":
strapptype = "application/ms-txt";
break;
case "html":
case "htm":
strapptype = "application/ms-html";
break;
default: return;
}
gridview mygridview = new gridview();
mygridview.datasource = dt;
mygridview.databind();
httpcontext.current.response.clear();
httpcontext.current.response.buffer = true;
httpcontext.current.response.addheader("content-type", "text/html; charset=gb2312");
httpcontext.current.response.appendheader("content-disposition", string.format("attachment;filename={0}.{1}", httputility.urlencode(strfile,encoding.getencoding("gb2312")), strext));
httpcontext.current.response.contentencoding = system.text.encoding.getencoding("gb2312");
httpcontext.current.response.contenttype = strapptype;
//mygridview.page.enableviewstate = false;
//二、定义一个输入流
system.globalization.cultureinfo mycitrad = new system.globalization.cultureinfo("zh-cn", true);
system.io.stringwriter ostringwriter = new system.io.stringwriter(mycitrad);
system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter);
//三、将目标数据绑定到输入流输出
mygridview.rendercontrol(ohtmltextwriter);
httpcontext.current.response.write(ostringwriter.tostring());
httpcontext.current.response.end();
}

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网