首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从C#打开Office2010文档时出错

从C#打开Office2010文档时出错
EN

Stack Overflow用户
提问于 2012-01-17 01:03:54
回答 1查看 2.9K关注 0票数 3

我正在尝试通过以下方式为用户加载office文档

代码语言:javascript
复制
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
HttpContext.Current.Response.ContentType = MIMETypesDictionary[fileExt];
HttpContext.Current.Response.BinaryWrite(fileArray);

我的MIMETypesDictionary有这个(缩写)

代码语言:javascript
复制
    private static readonly Dictionary<string, string> MIMETypesDictionary = new Dictionary<string, string>
      {
        {"doc", "application/msword"},
        {"docx", "application/vnd.openxmlformats-officedocument.wordprocessingml.document"},
        ...
        {"xls", "application/vnd.ms-excel"},  
        {"xlsx", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"}
        ...
      };

doc和xls文件工作得很好。客户端应用程序中的Docx和xlsx文件错误。

Excel错误

代码语言:javascript
复制
Excel found unreadable content in 'filename.xlsx'. Do you want to recover the contents of this workfbook?

单词错误

代码语言:javascript
复制
The file filename.docx cannot be opened because there are problems with the contents.

如果我单击Yes,它将加载,并且文档看起来没有问题。当然,这个错误对用户来说是不可能的。我是否对这些文件使用了正确的mime类型?我还尝试了application/vnd.ms-word.document.12、application/vnd.ms-excel.12和application/octet-stream,得到了相同的错误。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-01-17 01:24:49

解决了!必须构建响应代码

代码语言:javascript
复制
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name); 
HttpContext.Current.Response.ContentType = MIMETypesDictionary[fileExt];
HttpContext.Current.Response.BinaryWrite(fileArray);
HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();

我保留了文件类型的八位位流的值,它工作得很好!

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8883524

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档