//清除客户端当前显示
Response.Clear();
//作为附件输出,filename=FileFlow.xls 指定输出文件的名称,注意其扩展名和指定文件类型相符,可以为:.doc .xls .txt .htm
Response.AddHeader("content-disposition", "attachment;filename=callbackData.xls"); //显示标头
//设置显示的字和内容要存的形式
Response.Charset = "gb2312";
Response.ContentType = "application/vnd.xls";
//Response.ContentType = "application/octet-stream";(此句适合任何文件类型)
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
this.GridView1.Columns[0].Visible = false;//使第一列查看不会导入进去
GridView1.AllowPaging = false;
btnsearch_Click(null, null);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
GridView1.AllowPaging = true;
btnsearch_Click(null, null);
2.一定不能忘记这个方法否则就汇报(类型“GridView”的控件“GridView1”必须放在具有 runat=server 的窗体标记内 错误)
public override void VerifyRenderingInServerForm(Control control)
{
//base.VerifyRenderingInServerForm(control);
}