首页 新闻 会员 周边

.net导出excel时,如果文件名稍长,直接打开时提示“发生了DDE错误”

0
悬赏园豆:20 [已解决问题] 解决于 2008-12-22 15:02

在页面上下载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),导出打开正常,但会乱码?

 

 

飛雪飄寒的主页 飛雪飄寒 | 初学一级 | 园豆:30
提问于:2008-12-16 09:33
< >
分享
最佳答案
1

string filenamewithext=filename+".xls";

string savedName = HttpUtility.UrlEncode(filenamewithext.Replace("+", "%20");

Response.AddHeader("content-disposition", "attachment;filename=" + savedName);

这样子你试试看,怀疑是编码问题,如果必须要使用utf-8,测试完再加上试试!

GUO Xingwang | 老鸟四级 |园豆:3885 | 2008-12-16 09:44
其他回答(4)
0

看下面这个微软的说明,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.

eaglet | 园豆:17139 (专家六级) | 2008-12-16 09:45
0


        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不会乱码(文件名太长的时候文件名前面一部分会无法解码,后面正常~)

看后面评论中我的理解大家研究一下~

BB_Coder | 园豆:797 (小虾三级) | 2008-12-16 10:06
0
sl2008 | 园豆:407 (菜鸟二级) | 2008-12-16 19:35
0

System.Text.Encoding.UTF8),导出打开正常,但会乱码,那肯定就要用gb2312了,而且前面确实要用一个Response.Clear();

不过你那么长的文件名,即使不报错,看起来有意义吗?

有所为,有所不为 | 园豆:1200 (小虾三级) | 2008-12-17 10:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册