before this line Response.Write(tw.ToString()); you have to put this line Response.Write("<head><style> td {mso-number-format:\\@;} </style></head><body>");
that's it
You can see below.
Response.Write("<head><style> td {mso-number-format:\\@;} </style></head><body>");
Response.Write(tw.ToString());
You can also see whole code, It looks like
protected void ImgExcel_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=AddGoodsDetails_" + DateTime.Now + ".xls");
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
gvView_2.RenderControl(htmlWrite);
Response.Write("<head><style> td {mso-number-format:\\@;} </style></head><body>");
Response.Write(stringWrite.ToString());
Response.End();
}
gvView_2
is an id of GridView, you can understand i hope this solution will help in your problem.