Exporting contents to excel or preparing a excel report of the data displayed on a webpage is one of the most common tasks in real world web applications. In this article, i will implement the common ways of exporting a table of data displayed on a web page to a excel file.
public override void VerifyRenderingInServerForm(Control control) { }
protected void Button2_Click(object sender, EventArgs e)
{
string attachment = “attachment; filename=Timetable.xls”;
Response.ClearContent();
Response.AddHeader(“content-disposition”, attachment);
Response.ContentType = “application/ms-excel”;
StringWriter sw = new StringWriter();
HtmlTextWriter htw = new HtmlTextWriter(sw);
Gridview1.RenderControl(htw);
Response.Write(sw.ToString());
Response.End();
}