Hi,
In your code, while creating the stream writer,
StringWriter objSW = new StringWriter();
HtmlTextWriter objTW = new HtmlTextWriter(objSW);
That can be modified as
using (StringWriter sw = new StringWriter())
{
HtmlTextWriter hw = new HtmlTextWriter(sw);
GridView1.AllowPaging = false;
this.BindGrid();
foreach (GridViewRow row in GridView1.Rows)
{
}
gvDetails.AllowPaging = false;
BindDatatable();
gvDetails.Columns[4].Visible = false;
gvDetails.RenderControl(objTW);
Response.Write(objSW);
Response.End();
}
Or if looping through is much and you can make use of a excel template, then you can refer this article,
http://www.c-sharpcorner.com/UploadFile/pandeypradip/export-data-into-excel-in-a-pre-defined-template-using-strea/[
^]
This is not tested.Its just to give you an idea.
Hope this helps.
Happy coding!