Asp.NET导出Excel文件乱码 终极解决方法
System.Web.UI.WebControls.DataGrid datagrid=new System.Web.UI.WebControls.DataGrid();
datagrid.ID="excelGrid1";
datagrid.DataSource=data;
datagrid.DataBind();
string filename = "table.xls";
filename = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename));
//导出
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer= true;
System.Web.HttpContext.Current.Response.Charset="UTF-8"; //GB2312
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+filename);
System.Web.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
datagrid.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
datagrid.RenderControl(oHtmlTextWriter);
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
System.Web.HttpContext.Current.Response.End();
datagrid.ID="excelGrid1";
datagrid.DataSource=data;
datagrid.DataBind();
string filename = "table.xls";
filename = System.Web.HttpUtility.UrlEncode(System.Text.Encoding.UTF8.GetBytes(filename));
//导出
System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.Buffer= true;
System.Web.HttpContext.Current.Response.Charset="UTF-8"; //GB2312
System.Web.HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+filename);
System.Web.HttpContext.Current.Response.ContentEncoding=System.Text.Encoding.GetEncoding("UTF-8");//设置输出流为简体中文
System.Web.HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
datagrid.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN",true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
datagrid.RenderControl(oHtmlTextWriter);
System.Web.HttpContext.Current.Response.Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
System.Web.HttpContext.Current.Response.Write(oStringWriter.ToString());
System.Web.HttpContext.Current.Response.End();
重点在
Write("<meta http-equiv=\"content-type\" content=\"application/ms-excel; charset=UTF-8\"/>");
其他 Asp.NET导出Excel文件乱码解决若干方法 请参考
http://www.cnblogs.com/cbcye/archive/2009/05/26/1490451.html