Export to Excel
Ø
Rendering the Gridview Control to Excel
Ø
Rendering the underlying DataTable to Excel
Rendering the Gridview Control to Excel
This is one of the most commonly done approach where we
set MIME type and use Gridview’s RenderControl() method, similar to we do for a
Datagrid control.
string attachment = "attachment;
filename=Employee.xls";
Response.ClearContent();
Response.AddHeader("content-disposition",
attachment);
Response.ContentType = "application/ms-excel";
StringWriter stw = new StringWriter();
HtmlTextWriter htextw = new
HtmlTextWriter(stw);
gvEmployee.RenderControl(htextw);
Response.Write(stw.ToString());
Response.End();
When we execute the above code, it will give the
following error.
|