Search code examples
asp.netc#-4.0gridviewexport-to-excel

Export to Excel file name


I wrote this codes for export GridView to Excel :

System.Web.HttpContext.Current.Response.Clear();
Response.Buffer = true;
Response.ClearContent();
Response.ClearHeaders();
Response.Charset = System.Text.Encoding.UTF8.EncodingName;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.BinaryWrite(System.Text.Encoding.UTF8.GetPreamble());
string FileName = Title + ".xls";
StringWriter strwritter = new StringWriter();
HtmlTextWriter htmltextwrtter = new HtmlTextWriter(strwritter);
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName);

but file name does not show correctly for non english names(Such as Arabic). How I can save file with non english names?

enter image description here

thanks


Solution

  • Wrap file name With

    Server.UrlEncode
    

    for example:

    string FileName = Server.UrlEncode(Title + ".xls");
    

    and it works!!!!