参考了孟宪会在microsoft论坛的回答(http://social.microsoft.com/Forums/zh-CN/1761/thread/61df2773-c8ad-416e-9797-f28201145f8d)
关键是对文件名指定编码方式,就可以解决这个问题了。
另外Office2007时,如果Content-Disposition里不指定文件名及后缀名,输出的格式自动变成.zip了。
有关资料是这样解释的:(http://blog.minidx.com/2008/01/24/436.html)
以前的Office文档是100%的二进制格式。第三方的工具操作起来非常不方便,而 Office2007从整体上都是基于XML格式的,这里并不是说Office2007文档可以保存成XML格式。而是Office2007默认的文档格式就是XML的(Word的docx、Excel的xlsx等)。也许有人会感到奇怪,用文本编辑器打开docx后,显示的仍然是二进制格式,并不是什么 XML。其实docx并不是普通的XML格式,当然,也不只是一个XML文件,docx本质上是一个zip文件,里面有一系列的xml、目录和其他的文件。如果我们将docx改成zip。就可以用winzip等软件将其解开。
string vFullName = gaZouDT[0].FILEPATH;
string vExtendName = "";
string vOutputName = gaZouDT[0].REFERENCE;
switch (int.Parse(type))
{
case EXCLE:
case EXCLE_2007:
if (vFullName.EndsWith("xlsx"))
{
vExtendName = vOutputName.EndsWith(".xlsx") ? "" : ".xlsx";
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
else
{
vExtendName = vOutputName.EndsWith(".xls") ? "" : ".xls";
Response.ContentType = "application/vnd.ms-excel";
}
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}{1}", HttpUtility.UrlEncode(vOutputName, System.Text.UTF8Encoding.UTF8), vExtendName));
break;
case PDF:
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment");
break;
case WORD:
case WORD_2007:
if (vFullName.EndsWith("docx"))
{
vExtendName = vOutputName.EndsWith(".docx") ? "" : ".docx";
Response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
else
{
vExtendName = vOutputName.EndsWith(".doc") ? "" : ".doc";
Response.ContentType = "application/msword";
}
Response.AppendHeader("Content-Disposition", string.Format("attachment; filename={0}{1}", HttpUtility.UrlEncode(vOutputName, System.Text.UTF8Encoding.UTF8), vExtendName));
break;
}
Response.Charset = "UTF-8";
Response.TransmitFile(gaZouDT[0].FILEPATH);
Response.End();