asp- 导出GridView、其它控件到Excel(防止中文乱码)
/// /// 导出到EXCEL /// /// /// public void ToExcel(System.Web.UI.Control ctl, string FileName) { HttpContext.Current.Response.Charset = "GB2312"; // 或UTF-7 以防乱码 HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); //HttpContext.Current.Response.ContentType = "application/octet-stream"; HttpContext.Current.Response.ContentType = "application/vnd.ms-excel"; Response.Write("");//出现中文乱码加这句 HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(FileName)) + ".xls"); ctl.Page.EnableViewState = false; System.IO.StringWriter tw = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw); ctl.RenderControl(hw); HttpContext.Current.Response.Write(tw.ToString()); HttpContext.Current.Response.End(); ctl.Page.EnableViewState = false; }
版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。
暂时没有评论,来抢沙发吧~