C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
来源:互联网 发布:汉王电子白板软件下载 编辑:程序博客网 时间:2024/04/30 15:26
- 郁闷了一天终于搞定这个问题了,出现这个问题时候文件其实内容还是可以打开的,就是出现以上的错误原因。经过最终分析确定了具体原因,是因为在Response下载文档时候,最后需要结束
System.Web.HttpContext.Current.Response.End();否则默认为不完整下载状态。反正我加上去后就不出现以上情况了。具体代码如下:
/// <summary> /// 下载文件/// </summary> public void LoadPaperTemplate(string mStrFileName) { FileStream fs = null; BinaryReader br = null; BinaryWriter brnew = null; try { //给内容赋值 string path = System.Web.HttpContext.Current.Server.MapPath("~/Template"); string mStrFileRoot = string.Format("{0}\\{1}", path, mStrFileName); if (File.Exists(mStrFileRoot)) { fs = new System.IO.FileStream(mStrFileRoot, System.IO.FileMode.Open); br = new BinaryReader((Stream)fs); byte[] bytes = br.ReadBytes((Int32)fs.Length); brnew = new BinaryWriter(fs); brnew.Write(bytes, 0, bytes.Length); System.Web.HttpContext.Current.Response.Clear(); System.Web.HttpContext.Current.Response.Buffer = true; System.Web.HttpContext.Current.Response.Charset = "GB2312"; System.Web.HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(mStrFileRoot.Substring(mStrFileRoot.LastIndexOf('\\') + 1))); System.Web.HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.GetEncoding("utf-8"); System.Web.HttpContext.Current.Response.ContentType = "application/ms-word"; System.Web.HttpContext.Current.Response.BinaryWrite(bytes); System.Web.HttpContext.Current.Response.Flush(); System.Web.HttpContext.Current.Response.End(); } } catch (Exception) { //throw; } finally { br.Close(); brnew.Close(); fs.Close(); } }
转载地址:http://www.cnblogs.com/BeyondWJsel/archive/2012/05/10/2494418.html
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- java创建word打开后弹出无法打开office open xml文件....因为内容有错误提示
- word2007 -----无法打开Office OpenXML 文件《文件名》, 因为内容有错误。
- word2007 -----无法打开Office OpenXML 文件《文件名》, 因为内容有错误。 .
- office xls 文件已损坏 无法打开 word在试图打开文件时遇到错误
- java freemarker 通过ftl模板文件导出word文件发现在有中文地方出现在乱码,打开word文件提示xml错误解决办法
- PHPWord 导出模版Word文件,无法打开,提示xml pasring error
- Microsoft word “很抱歉,无法打开xxx.docx,因为内容有问题” 的解决办法
- 关于项目文件导入打开报:无法识别元素 <Target> 中的属性“Returns”错误解决方法
- 关于项目文件导入打开报:无法识别元素 <Target> 中的属性“Returns”错误解决方法
- .net office excel文件导出 dcom open xml sdk 和 npoi 导出对比总结
- 读取XML文件时,总报“前言中不允许有内容”错误-分析
- chm 文件 打开有目录无内容的解决方法
- Word无法打开该文件,因为文件格式与文件扩展名不匹配的解决办法
- ie无法直接打开xml 文件的解决方法:
- 使用IE浏览器无法打开xml文件解决方法
- 使用IE浏览器无法打开xml文件解决方法
- 无法打开chm文件解决方法
- 布局中的错误提示
- Linux 2.6 menuconfig内核编译配置选项详解
- 八皇后问题解法
- 使用Oracle Database 11g可以创建Interval分区表
- C/C++ 解析“extern”
- C# 导出Word报”无法打开Office open xml文件。因为文件内容有错误“ 解决方法
- STM8 UART2
- pl/sql 定义函数例子
- 在iphone上设置邮箱
- android.view.WindowManager$BadTokenException: ... is your activity running? 异常问题
- 数据库建模&UML建模
- AJax视频教程适高级篇+源码(2G)25讲
- 恒润电子的电话面试
- YUI概览