sub btn1_click(sender as object,e as eventargs)
    HttpContext.Current.Response.AppendHeader("Content-   Disposition", "attachment;filename=Excel.xls") 
   Response.ContentType = "application nd.ms-excel" 
   HttpContext.Current.Response.Charset = "GB2312" 
   HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default 
   mygrid.Page.EnableViewState = False 
  Dim tw As System.IO.StringWriter = New System.IO.StringWriter () 
   Dim hw As System.Web.UI.HtmlTextWriter = New System.Web.UI.HtmlTextWriter (tw) 
   mygrid.Visible = True 
   mygrid.RenderControl(hw) 
   HttpContext.Current.Response.Write(tw.ToString()) 
   HttpContext.Current.Response.End()
 end sub 
我用这段代码把DATAGRID 转到一个。XLS文件里 但是datagrid 中的中文都变成乱码了 谁知道要怎么弄了吗 ??小弟诚心求教 各位大哥帮帮我啊 感激不尽

解决方案 »

  1.   

    我也遇到这个问题,改 utf-8
    有时gb2312乱码,有时utf-8乱码,一气之下我写两个方法出乱码我就换一个:(
      

  2.   

    Web.config默认是UTF-8,不过如果页面上注明是GB2312的话,就用2312了。
      

  3.   

    //将HTTP头添加到输出流
    HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=execl.xls"); //指定生成文件的类型(要转化为其它类型文件在这里更改,同时还要更改上一句filename)
    Response.ContentType = "application/vnd.ms-execl";  
    HttpContext.Current.Response.Charset = "";
    HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
    this.Datagrid2.Page.EnableViewState = true; System.IO.StringWriter tw= new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter hw= new System.Web.UI.HtmlTextWriter(tw);
    this.Datagrid1.Visible = true;

    //将服务器控件中的内容输出到System.Web.UI.HtmlTextWriter对象中
    this.Datagrid1.RenderControl(hw);

    HttpContext.Current.Response.Write(tw.ToString());
    HttpContext.Current.Response.End();
      

  4.   

    HttpContext.Current.Response.Charset = "" 
    HttpContext.Current.Response.ContentEncoding = (web.config里面设置的编码)