Export To Excel Even With Special Character in Data

The Following C# code is used to Export data from ASP.NET to Excel with special characters

try
{
Response.ContentType = "application/vnd.ms-excel 8.0";
Response.Charset = "";
Response.ContentEncoding = System.Text.Encoding.Default;

this.EnableViewState = false;

System.IO.StringWriter ObjWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter ObjHtmlTextWriter = new System.Web.UI.HtmlTextWriter(ObjWriter);

rptUserDetails.RenderControl(ObjHtmlTextWriter);
//dgdInitiative is the Grid name which is to be exported

Response.Write(ObjWriter.ToString());
string strhtml = ObjHtmlTextWriter.ToString();
Response.AppendHeader("content-disposition", "attachment;filename=Users.xls");
Response.End();
}
catch (System.Exception ex)
{
string strMsg = ex.Message;
}

Comments

Anonymous said…
This solved my illegal chars appearing in exported excel file. They are appearing properly now.
Thanks a lot.
Anonymous said…
Thank you so much! this one was hard to find :)

Popular posts from this blog

Get Plain Text from FCKEditor in any Browser.