The code to do the Excel Export is very straightforward. You can also export to different application type by changing the content-disposition and ContentType.
string attachment = "attachment; filename=Contacts.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();
If you run the code as above, it will result in an HttpException as follows:
Control 'GridView1' of type 'GridView' must be placed inside a form tag with runat=server."
To avoid this error, add the following code:
public override void VerifyRenderingInServerForm(Control control)
{
}
1 comment:
Good collection you have, increase this so that everyone will get helped by you.
Post a Comment