Export ASP.NET Gridview data into MS-Excel sheet
HtmlForm class: HtmlForm provides programmatic access to the HTML <form> element on the server through the set of properties. Namespace: using System.Web.UI.HtmlControls; Code under Print button: protected void btnPrint_Click(object sender, EventArgs e) { ExportFromHtmlForm(gvPrint); }
public void ExportFromHtmlForm(GridView gv) { HtmlForm form = new HtmlForm();
string attachment = "attachment; filename=PrintDetails.xls"; Response.ClearContent(); Response.AddHeader("content-disposition", attachment); Response.ContentType = "application/ms-excel";
//namespace (using system.IO) StringWriter stw = new StringWriter(); HtmlTextWriter htextw = new HtmlTextWriter(stw);
gv.Parent.Controls.Add(form); form.Attributes["runat"] = "server"; form.Controls.Add(gv); this.Controls.Add(form); form.RenderControl(htextw); Response.Write(stw.ToString());
Response.End(); }
You need to be a premium member to use this feature. To access it, you'll have to upgrade your membership.
Become a sharper developer and jumpstart your career.
$0
$
. 00
monthly
For Basic members:
$20
For Premium members:
$45
For Elite members: