private void ExportToExcel() { Response.Clear(); Response.Charset = "GB2312"; Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312"); string FileName = "文件名.xls"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8).ToString()); Response.ContentType = "application/ms-excel"; this.EnableViewState = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); GridView1.AllowPaging = false; GVDataBind(); //GridView绑定数据操作 GridView1.Columns[6].Visible = false; GridView1.Columns[7].Visible = false; GridView1.RenderControl(hw); Response.Write(tw.ToString()); Response.End(); GridView1.AllowPaging = true; GVDataBind(); }
//这个方法必须加,否则会报错! public override void VerifyRenderingInServerForm(Control control) { }