当前位置: 代码迷 >> ASP.NET >> 关于datagirdview 导出excel乱码有关问题
  详细解决方案

关于datagirdview 导出excel乱码有关问题

热度:3913   发布时间:2013-02-25 00:00:00.0
关于datagirdview 导出excel乱码问题
C# code
    public static void ExportExcel(GridView gridview,string filename,DataBindGridView databindgridview)    {        HttpContext.Current.Response.Clear();        HttpContext.Current.Response.Buffer = true;        HttpContext.Current.Response.Charset = "GB2312";        HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename + ".xls");        //System.Text.Encoding.GetEncoding("GB2312")        //HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls\"");        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!        HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); //System.Text.Encoding.GetEncoding("GB2312");//System.Text.Encoding.UTF8;        HttpContext.Current.Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。         //System.IO.StringWriter oStringWriter = new System.IO.StringWriter();        gridview.EnableViewState = false;        //this.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);        databindgridview();        gridview.RenderControl(oHtmlTextWriter);        HttpContext.Current.Response.Output.Write(oStringWriter.ToString());        HttpContext.Current.Response.Flush();        HttpContext.Current.Response.End();    }

这个是代码,导出来中文是乱码,网上的解决办法我也用了,可是还是乱码请教大虾们,代码哪里有误吗?

------解决方案--------------------------------------------------------
HttpContext.Current.Response.Charset = "GB2312";HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");

这两个值统一一下,试试呢?
------解决方案--------------------------------------------------------
探讨
C# code
public static void ExportExcel(GridView gridview,string filename,DataBindGridView databindgridview)
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Respons……

------解决方案--------------------------------------------------------
全部用utf-8就可以了
------解决方案--------------------------------------------------------
C# code
 //System.Text.Encoding.GetEncoding("GB2312")        //HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls\"");        // 如果设置为 GetEncoding("GB2312");导出的文件将会出现乱码!!!
------解决方案--------------------------------------------------------
探讨

引用:

C# code
//System.Text.Encoding.GetEncoding("GB2312")
//HttpContext.Current.Response.AppendHeader("content-disposition", "attachment;filename=\"" + System.Web.HttpUtility.UrlEncode(f……

------解决方案--------------------------------------------------------
我这里有多种解决方案,你可以选择自己合适的方法。代码如下:
1、由dataset生成

以下为引用的内容:
public void CreateExcel(DataSet ds,string typeid,string FileName)

{

HttpResponse resp;

resp = Page.Response;

resp.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
  相关解决方案