Response.Buffer = true; 
            Response.Charset = "utf-8"; 
            Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("Excel.xls", System.Text.Encoding.GetEncoding("GB2312"))); 
                  Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
            Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。 
            System.IO.StringWriter oStringWriter = new System.IO.StringWriter(); 
            System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter); 
            this.GridView1.RenderControl(oHtmlTextWriter); 
            Response.Write(" <meta  http-equiv=Content-Type  content=text/html;charset=GB2312>"); 
            Response.Output.Write(oStringWriter.ToString()); 
            Response.Flush(); 
            Response.End();
为什么在IE下有这种情况firefox下正常,与浏览器有关系吗?

解决方案 »

  1.   

    http://fengyp.blog.51cto.com/276754/51812
      

  2.   

    将你代码中的这句:
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); 
    该为:
    Response.ContentEncoding = System.Text.Encoding.Default; 
    试试看,肯定能解决的!
      

  3.   

    应该是编码问题,xls转sql也会有类似情况。
      

  4.   

    要不在Web.Config文件中添加以下代码:
    <globalization requestEncoding="gb2312" responseEncoding="gb2312"/>
    看看?
      

  5.   

    public void ExportExcel(DataTable table,string filepath)
            {
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
                DataGrid excel = new DataGrid();
                System.Web.UI.WebControls.TableItemStyle AlternatingStyle = new TableItemStyle();
                System.Web.UI.WebControls.TableItemStyle headerStyle = new TableItemStyle();
                System.Web.UI.WebControls.TableItemStyle itemStyle = new TableItemStyle();
                AlternatingStyle.BackColor = System.Drawing.Color.LightGray;
                headerStyle.BackColor = System.Drawing.Color.LightGray;
                headerStyle.Font.Bold = true;
                headerStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center;
                itemStyle.HorizontalAlign = System.Web.UI.WebControls.HorizontalAlign.Center; ;            excel.AlternatingItemStyle.MergeWith(AlternatingStyle);
                excel.HeaderStyle.MergeWith(headerStyle);
                excel.ItemStyle.MergeWith(itemStyle);
                excel.GridLines = GridLines.Both;
                excel.HeaderStyle.Font.Bold = true;
                excel.DataSource = table.DefaultView;   //输出DataTable的内容
                excel.DataBind();
                excel.RenderControl(htmlWriter);            string filestr = filepath;
                int pos = filestr.LastIndexOf("\\");
                string file = filestr.Substring(0, pos);
                if (!Directory.Exists(file))
                {
                    Directory.CreateDirectory(file);
                }
                System.IO.StreamWriter sw = new StreamWriter(filestr);
                sw.Write(stringWriter.ToString());
                sw.Close();
            }
    我前两天做的,导出excel 是正确的,可以参考一下