通过GridView的RenderControl实现导出Excel时,在客户端有的电脑导出的是正确的,有的电脑导出的是.aspx中的几个控件或乱码?
代码是从网上拷的,不知道哪错了,有没有谁遇到过这个问题?
GridView1.DataSource = t.Tables[0];
GridView1.DataBind();
System.Web.UI.Control ctl = this.GridView1;
//DataGrid1是你在窗体中拖放的控件
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=Excel.xls");
HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Default;
HttpContext.Current.Response.ContentType = "application/ms-excel";
Response.Write("<meta http-equiv=Content-Type content=\"text/html; charset=GB2312\">");
ctl.Page.EnableViewState = false;
System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
ctl.RenderControl(hw);
HttpContext.Current.Response.Write(tw.ToString());
HttpContext.Current.Response.End();
不要直接用控件来导出 Excel,导出的Excel不是真正的Excel格式。用 NPOI 组件来导出EXCEL。