导出excel问题:
朋友们在导出时,可能也会遇到乱码的问题,松子儿的解决方法如下:
下面是一段正确的导出按钮事件代码
private void btnMIME_Click(object sender, System.EventArgs e)
...{
Response.ContentType = "application/vnd.ms-excel";//响应类型
Response.Charset = "";//响应编码类型
Response.ContentEncoding = Encoding.UTF7;//注意:UTF8是不支持简体中文的
this.EnableViewState = false;
System.IO.StringWriter sw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(sw);
int nCur = dgShow.CurrentPageIndex;
int nSize = dgShow.PageSize;
![]()
dgShow.AllowPaging = false;
BindData();
![]()
dgShow.Columns[7].Visible =false;
dgShow.RenderControl(hw);
dgShow.Columns[7].Visible =true;
![]()
//以下恢复分页
dgShow.AllowPaging = true;
dgShow.CurrentPageIndex = nCur;
dgShow.PageSize = nSize;
BindData();
Response.Write(sw.ToString());
Response.End();
}
注意:设置输出流的http字符集(Response.ContentEncoding),“urf8”:不支持简体中文;“gb2312”:excel不支持,会报错!