欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Asp.net Gridview 导出到Excel[解决编码乱码问题]

程序员文章站 2022-07-01 18:27:57
[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(); 
    }