asp .net 导出excel 360 浏览器 、ie 导出的文件名是正确的,但火狐去又是乱码了。

asp .net 导出excel 360 浏览器 、ie 导出的文件名是正确的,但火狐去又是乱码了。有时候360 和ie 却 导出了大堆页面控件 ,而火狐却是好好的。 DateTime dt = DateTime.Now; string filename = dt.Year.ToString() + dt.Month.ToString() + dt.Day.ToString() + dt.Hour.ToString() + dt.Minute.ToString() + dt.Second.ToString(); if (dte.Rows.Count > 0) { Response.Clear(); Response.ClearContent(); Response.Charset = "utf-8"; Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("货品资料", System.Text.Encoding.UTF8) + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8) + ".xls"); Response.ContentEncoding = System.Text.Encoding.UTF8; Response.ContentType = "application/ms-excel"; StringWriter sw = new StringWriter(); GridView gv = new GridView(); stei(dte); gv.DataSource = dte; gv.DataBind(); this.Page.EnableViewState = false; HtmlTextWriter htw = new HtmlTextWriter(sw); gv.RenderControl(htw); Response.Write(sw.ToString()); Response.Flush(); Response.End(); } else { Response.Write("没有数据"); }
2026年09月28日 03:19
有2个网友回答
网友(1):

  尊敬的用户,您好!很高兴为您答疑
  据您的源码来看,您在写实体文件名的时候对文件名进行了修码。如果您在导出excel文件时,乱码的情况很严重,建议考虑使用非双字节的文件名。
  如果对于中文名有特别需求,建议您尝试使用Server.UrlEncode()方法,以默认的编码对URL进行编码。
  希望我的回答对您有所帮助,如有疑问,欢迎继续咨询我们。

网友(2):

你这个HttpUtility.UrlEncode 进行了URL编码了,输出自然是乱码,一般跨页面地址栏传输数据首先在本页面进行URL编码,然后再在跳转页解码HttpUtility.UrlDecode 才可以,你这个本页面导出为啥还要编码呢 我是直接 文件名.xls 保存