GridView 生成 Excel
我在生成Excel的时候总是说我类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内。
但是我已经放进去了啊.怎么总是报这个错误呢?请各位大狭帮个忙.代码如下
protected void btBuild_Click(object sender, EventArgs e)
{
Response.Clear();
Response.Buffer = true;
Response.Write("<meta http-equiv=Content-Type content=text/html;charset=gb2312>");
Response.AppendHeader("content-disposition", "attachment;filename=\"" + HttpUtility.UrlEncode("生成报表[" + DateTime.Now.ToString("yyyy-MM-dd") + "]", System.Text.Encoding.UTF8) + ".xls\"");
//Response.Charset="GB2312";
Response.AppendHeader("Content-Disposition", "attachment;filename=FileName.xls");
Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");
//设置输出流为简体中文
//Response.ContentType = "application/ms-excel";//设置输出文件类型为excel文件。
this.EnableViewState = false;
System.Globalization.CultureInfo myCItrad = new System.Globalization.CultureInfo("ZH-CN", true);
System.IO.StringWriter oStringWriter = new System.IO.StringWriter(myCItrad);
System.Web.UI.HtmlTextWriter oHtmlTextWriter = new System.Web.UI.HtmlTextWriter(oStringWriter);
this.GridView1.RenderControl(oHtmlTextWriter);
Response.Write(oStringWriter.ToString());
Response.End();
}
------解决方案--------------------
我遇到过这个问题
加 jenkin1016 说的
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}
就可以了。