日期:2014-05-16  浏览次数:20474 次

GridView导出生成Excel文件的问题,office2007/2010打不开 ????
GridView导出生成Excel文件的问题,office2007/2010打不开 ????

2003的代码
 Response.Clear();
                Response.Buffer = true;
                Response.Charset = "GB2312";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlPathEncode(HeaderLabel.Text + ".xls"));
                Response.ContentEncoding = System.Text.Encoding.UTF7;
                Response.ContentType = "application/ms-excel";
                System.IO.StringWriter oStringWriter = new System.IO.StringWriter();
                System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
                gridview.RenderControl(oHtmlTextWriter);
                Response.Output.Write(oStringWriter.ToString());
                Response.Flush();
                Response.End();


2010的代码

       Response.Charset = "GB2312";
                Response.ContentEncoding = System.Text.Encoding.UTF8;
                //Response.Write("<meta http-equiv=Content-Type content=text/html;charset=UTF-8>");
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(HeaderLabel.Text + ".xlsx", System.Text.Encoding.UTF8).ToString());
                Response.ContentType = "application/ms-excel"; ;
                this.EnableViewState = false;
                System.IO.StringWriter tw = new System.IO.StringWriter();
                HtmlTextWriter hw = new HtmlTextWriter(tw);
                gridview.RenderControl(hw);
                Response.Write(tw.ToString());
                Response.End();

这样写也不行呢!
------解决方案--------------------

 /// <summary>
    /// 定义导出Excel的函数