急啊 在线等啊 ~~~~ 导出问题

duzidengdai521 2009-01-06 05:15:54
比如本来我的中文名是“列表.xls”,但是导出的excel中的标题和工作表名都成了:%e5%88%97%e8%a1%a8 1 .xls ,如果用英文名就没有问题,试了
HttpUtility.UrlEncode的方法也不行,在线等各位高手解答


代码如下:
public static void DtToExcel(DataTable dt, string FileName)
{
System.Web.HttpResponse httpResponse = System.Web.HttpContext.Current.Response;


httpResponse.Clear();
System.Web.UI.WebControls.GridView dataGrid = new System.Web.UI.WebControls.GridView();
dataGrid.DataSource = dt;
dataGrid.AllowPaging = false;
dataGrid.HeaderStyle.BackColor = System.Drawing.Color.White;
dataGrid.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
dataGrid.HeaderStyle.Font.Bold = true;
dataGrid.RowDataBound += new GridViewRowEventHandler(dataGrid_RowDataBound);
dataGrid.DataBind();

string aa = HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);




httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8)); //filename="*.xls";
//httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + FileName);


//httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");




httpResponse.ContentType = "application/vnd.ms-excel";


System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
dataGrid.RenderControl(hw);
httpResponse.Write(tw.ToString());
httpResponse.End();

}
...全文
76 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
duzidengdai521 2009-01-06
  • 打赏
  • 举报
回复
不行啊 标题还都是乱码啊 在线等。。。。。。。。。。。。。。。。。。。
nj_1st_excellence 2009-01-06
  • 打赏
  • 举报
回复
可以用下面2种简单的方法:

导出的类部分代码:
HttpResponse resp;
resp=Page.Response;
resp.ContentEncoding=System.Text.Encoding.Default;
resp.AppendHeader("Content-Disposition", "attachment;filename=报表.xls"); //这样写就出现了乱码


1.用System.Web.HttpUtility.UrlEncode或者Server.UrlEncode方法,不过要2个参数都写上:
System.Web.HttpUtility.UrlEncode("报表",System.Text.Encoding.UTF8)+".xls");

2.用HttpUtility.UrlPathEncode方法,只要写一个参数就可以了:
HttpUtility.UrlPathEncode("报表.xls")

最好就是这种了:
resp.AppendHeader("Content-Disposition", "attachment;filename="+HttpUtility.UrlPathEncode(FileName));

nj_1st_excellence 2009-01-06
  • 打赏
  • 举报
回复
string aa = HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8);
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
-->
string aa = HttpUtility.UrlEncode(FileName, System.Text.Encoding.GB2312);
httpResponse.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.GB2312));

httpResponse.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");//设置输出流为简体中文

试试

62,269

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧