Asp.net Gridview 导出到Excel[解决编码乱码问题]
程序员文章站
2022-03-26 19:24:30
[csharp]
public override void verifyrenderinginserverform(control control)
...
[csharp]
public override void verifyrenderinginserverform(control control)
{ }
protected void linkbutton2_click(object sender, eventargs e)
{
response.clear();
response.buffer = true;
response.charset = "gb2312";
response.contentencoding = system.text.encoding.getencoding("gb2312");
response.appendheader("content-disposition", "attachment;filename=\"" + system.web.httputility.urlencode("数据导出", system.text.encoding.utf8) + ".xls\"");
response.contenttype = "application/ms-excel";
system.io.stringwriter ostringwriter = new system.io.stringwriter();
system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter);
this.label1.rendercontrol(ohtmltextwriter);
this.gridview1.rendercontrol(ohtmltextwriter);
response.output.write(ostringwriter.tostring());
response.flush();
response.end();
}
public override void verifyrenderinginserverform(control control)
{ }
protected void linkbutton2_click(object sender, eventargs e)
{
response.clear();
response.buffer = true;
response.charset = "gb2312";
response.contentencoding = system.text.encoding.getencoding("gb2312");
response.appendheader("content-disposition", "attachment;filename=\"" + system.web.httputility.urlencode("数据导出", system.text.encoding.utf8) + ".xls\"");
response.contenttype = "application/ms-excel";
system.io.stringwriter ostringwriter = new system.io.stringwriter();
system.web.ui.htmltextwriter ohtmltextwriter = new system.web.ui.htmltextwriter(ostringwriter);
this.label1.rendercontrol(ohtmltextwriter);
this.gridview1.rendercontrol(ohtmltextwriter);
response.output.write(ostringwriter.tostring());
response.flush();
response.end();
}