在页面上下载EXECL文件时出错: 发生了DDE错误,并且该错误的说明由于太长而不能显示。如果文件名或路径太长,请尝试重新命名该文件或将其复制到其他文件夹中。可是同样的路径同样的文件名下载WORD或者图片文件都没问题,而且只有EXECL文件会出这个错,请问这个问题如何解决?
源代码:
public void ExportControl(System.Web.UI.Control source, string DocumentType, string filename)
{
//设置Http的头信息,编码格式
if (DocumentType == "Excel")
{
//Excel
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".xls",System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
} HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.UTF8;
//关闭控件的视图状态
source.Page.EnableViewState =false;
//初始化HtmlWriter
System.IO.StringWriter writer = new System.IO.StringWriter() ;
System.Web.UI.HtmlTextWriter htmlWriter = new System.Web.UI.HtmlTextWriter(writer);
source.RenderControl(htmlWriter);
//输出
HttpContext.Current.Response.Write(writer.ToString());
HttpContext.Current.Response.End();
}
出错的地方在:HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+ HttpUtility.UrlEncode(filename+".xls",System.Text.Encoding.UTF8));
HttpContext.Current.Response.ContentType = "application/ms-excel";
如果文件名过长就会报错,如果改为filename="+ filename+".xls",System.Text.Encoding.UTF8),导出打开正常,但会乱码?
string filenamewithext=filename+".xls";
string savedName = HttpUtility.UrlEncode(filenamewithext.Replace("+", "%20");
Response.AddHeader("content-disposition", "attachment;filename=" + savedName);
这样子你试试看,怀疑是编码问题,如果必须要使用utf-8,测试完再加上试试!
看下面这个微软的说明,word 文档的最大路径长度为254字符,Excel为218,这就是为什么word可以,Excel不可以的原因。我觉得没有其他办法,只有通过缩短文件路径名来解决。 http://support.microsoft.com/kb/325573 his issue occurs because of a 256-character limitation on
creating and saving files in the Office products. Also, you receive an error message
when you save or open a file if the path of the file meets the following
situations.
Microsoft Word: The total length of the path and the file
name, including file name extension, exceeds 254 characters.
Microsoft PowerPoint: The total length of the path and the file name, including file
name extension, exceeds 258 characters.
Microsoft Access: The total
length of the path and the file name, including file name extension exceeds 258
characters.
Microsoft Outlook: The total length of the path and the file
name, including file name extension, exceeds 258 characters.
Microsoft Excel: The total length of the path and the file name, including file name
extension, exceeds 218 characters.
Note This limitation includes the three characters that represent the
drive, the characters in folder names, the backslash character between folders,
and the characters in the file name.
Response.Clear();//这里也是很重要的~不然会把页面的元素也输出~
Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.ContentType = "application/MSExcel";
Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
Response.Write(excelContent);
Response.End();
我导出这样写的。这是很久以前写的,现在用的也是这个~。
我也不知道为什么两个地方Encoding要不同,不过导出的Excel不会乱码(文件名太长的时候文件名前面一部分会无法解码,后面正常~)
看后面评论中我的理解大家研究一下~
System.Text.Encoding.UTF8),导出打开正常,但会乱码,那肯定就要用gb2312了,而且前面确实要用一个Response.Clear();
不过你那么长的文件名,即使不报错,看起来有意义吗?