You want Export HTML table (Not Gridview) customized structure and data to Excel using ASP.NET.
Try the following Approach
-
Provide the
ID
and addrunat="server"
attribute<table id="tbl" runat="server" >
-
Add the following code
Response.ContentType = "application/x-msexcel"; Response.AddHeader("Content-Disposition", "attachment; filename=ExcelFile.xls"); Response.ContentEncoding = Encoding.UTF8; StringWriter tw = new StringWriter(); HtmlTextWriter hw = new HtmlTextWriter(tw); tbl.RenderControl(hw); Response.Write(tw.ToString()); Response.End();