protected void Button3_Click(object sender, EventArgs e) { ExportGridView2Excel(GridView1, "test.xls"); } private void ExportGridView2Excel(GridView gridViewControl, string excelFileName) { if (string.IsNullOrEmpty(excelFileName)) throw new Exception("excelFileName is missing"); if (!excelFileName.EndsWith(".xls")) excelFileName += ".xls"; gridViewControl.AllowPaging = false; gridViewControl.AllowSorting = false; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); Response.Clear(); Response.ContentType = "application/vnd.ms-excel"; Response.AddHeader( "content-disposition", "attachment;filename=" + excelFileName); gridViewControl.RenderControl(hw); // execption in this line Response.Write(tw.ToString()); Response.End(); } public override void VerifyRenderingInServerForm(Control control) { return; }
EnableEventValidation="false"
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)