Gridview 导出 Excel2010 ,不是乱码就是资源版本的问题,
请教高手,如何避免上述错误呢

解决方案 »

  1.   

    2010?
    还没用过
    乱码 你就设置一下编码看看Response.Charset="GB2312";
      

  2.   

    Excel2010采用了不同的格式,标准的格式你需要采用Open XML、OpenXML或OOXML的格式生成才是最标准的xlsx文件,
    参见
    http://msdn.microsoft.com/en-us/office/bb265236另外,你也可以采用兼容的xml格式,可以设置样式、合并行列等
    参见
    http://dotnet.aspx.cc/file/Export-Gridview-To-Excel-With-Multi-Sheet.aspx
      

  3.   

    搞定了..
     string style = @"<style> .text { mso-number-format:\@; } </script> "; 
                    string s = this.ddlYWName.Text;
                    Response.Clear();
                    //获取或设置一个值,该值指示是否缓冲输出,并在完成处理整个响应之后将其发送
                    Response.Buffer = true;
                    //获取或设置输出流的HTTP字符集
                    Response.Charset = "GB2312";
                    // filename = System.Web.HttpUtility.UrlEncode(filename, System.Text.Encoding.GetEncoding("GB2312 "));
                    //将HTTP头添加到输出流
                    Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(s + ".xls", Encoding.UTF8).ToString());
                    //获取或设置输出流的HTTP字符集
                    Response.ContentEncoding = System.Text.Encoding.UTF7;
                    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
                    //获取或设置输出流的HTTP MIME类型
                   // Response.ContentType = "application/ms-excel";                Response.ContentType = "application/vnd.xls";                System.IO.StringWriter onstringwriter = new System.IO.StringWriter();
                    System.Web.UI.HtmlTextWriter onhtmltextwriter = new System.Web.UI.HtmlTextWriter(onstringwriter);
                    StringWriter sw = new StringWriter();
                    Response.Write(style); 
                    HtmlTextWriter htw = new HtmlTextWriter(sw);
                    this.gdvNonBindData.RenderControl(htw);
                    string html = sw.ToString().Trim();
                    Response.Output.Write(html);
                    Response.Flush();
                    Response.End(); public override void VerifyRenderingInServerForm(Control control)
            {
                //base.VerifyRenderingInServerForm(control);
            }