当前位置 : 主页 > 编程语言 > java >

asp.net 导出GridView、其它控件到Excel(防止中文乱码)

来源:互联网 收集:自由互联 发布时间:2022-07-22
/// summary /// 导出到EXCEL /// /summary /// param name="ctl"/param /// param name="FileName"/param public void ToExcel ( System . Web . UI . Control ctl , string FileName ) { HttpContext . Current . Response . Charset = "GB2312" ; // 或
/// <summary>
/// 导出到EXCEL
/// </summary>
/// <param name="ctl"></param>
/// <param name="FileName"></param>
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("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");//出现中文乱码加这句
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;
} 【文章转自bgp服务器 http://www.558idc.com/yz.html提供,感恩】
上一篇:TreeSet
下一篇:没有了
网友评论