导出到Excel文件名

时间:2015-08-24 04:52:28

标签: asp.net c#-4.0 gridview export-to-excel

我将此代码编写为导出GridView到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);

但是非英文名称(例如阿拉伯语)的文件名无法正确显示。如何保存非英文名称的文件?

enter image description here

感谢

1 个答案:

答案 0 :(得分:0)

使用

换行文件名
Server.UrlEncode

例如:

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

它有效!!!!