最近做项目需要用到word生成报表,按照网上的方法,现在项目的引用里面添加COM组件: Microsoft Word 14.0 Object Library,会在引用下面自动添加
Microsoft.Office.Interop.Word;和Microsoft.Office.Core,
(一)doc和docx的区别:docx和doc的区别显著的一点就是体积大小的不同。当我们创建一个空的文档文件的时候,我们发现,doc格式是25KB左右,而docx是0字节。docx格式就是一个zip文件,我们可以拿winrar打开docx文件,得到一堆的文件,很类似android程序或者win7桌面窗体的源码,你在里面可以找到各种配置文件,文本文件和媒体文件。
(二)使用下面这个代码,可以保存.doc文档,但是保存.docx文档时,在SaveDocument(string filePath)的wordDoc.SaveAs(…)会报错:不兼容的文档。修改成wordDoc.SaveAs(ref fileName)就可以保存docx文档了,其实是跟format = WdSaveFormat.wdFormatDocument;这个格式有关,改成object format = WdSaveFormat.wdFormatDocumentDefault;就可以保存docx文档了,wordDoc.SaveAs(ref fileName)相当于使用这个默认格式。

object format = WdSaveFormat.wdFormatDocument; //保存格式
object missing = System.Reflection.Missing.Value;
wordDoc.SaveAs(ref fileName, ref format, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing);
class TransientReport{private _Application wordApp = null; //定义应用程序对象private _Document wordDoc = null;    public _Application Applicaton{get { return wordApp; }set { wordApp = value; }}/// <summary>/// 文档对象/// </summary>public _Document WordDoc{get { return wordDoc; }set { wordDoc = value; }}public TransientReport() { }/// <summary>/// 通过模板创建新文件/// </summary>/// <param name="filePath"></param>public void CreateNewDocument(string filePath){killWnWordProcess();wordApp = new Application();wordApp.DisplayAlerts = WdAlertLevel.wdAlertsNone;wordApp.Visible = false;object missing = System.Reflection.Missing.Value;object templateName = filePath;wordDoc = wordApp.Documents.Open(ref templateName, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing);}/// <summary>/// 另存为文件/// </summary>/// <param name="filePath"></param>public void SaveDocument(string filePath){object fileName = filePath;object format = WdSaveFormat.wdFormatDocument; //保存格式object missing = System.Reflection.Missing.Value;wordDoc.SaveAs(ref fileName, ref format, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing);//关闭wordDoc,wordApp对象object saveChanges = WdSaveOptions.wdSaveChanges;object originalFormat = WdOriginalFormat.wdOriginalDocumentFormat;object routeDocument = false;wordDoc.Close(ref saveChanges, ref originalFormat, ref routeDocument);wordApp.Quit(ref saveChanges, ref originalFormat, ref routeDocument);}/// <summary>/// 写数据/// </summary>/// <param name="ds"></param>public void writeWord(DataSet[] ds){wordDoc.Content.Tables[1].Cell(1, 1).Range.Text = "123";}}

C#自带word COM保存docx文档时文件不兼容相关推荐

  1. 保存Excel文档时提示文档未保存

    作者:iamlaosong 保存Excel文档时,提示文档未保存,也无法另存,只能将内容复制到其他文件里保存,这个现象以前出现过,一般都能找到原因,这一次文件是别人发给我的,修改后就是无法保存,最后只 ...

  2. 使用Python批量提取并保存docx文档中的图片

    问题描述: 提取docx文档中的所有图片,保存为独立的图片文件. 技术要点: 需要安装扩展库python-docx 示例文件: 参考代码: 码运行结果: 神操作: 如果实在看不懂上面的代码,但是又有同 ...

  3. 针对word.docx文档的关键词索引器

    前言 之前曾想做这样一个工具,用来遍历目录下的word.docx文档查询关键词,主要是有几个目的: 1.在项目开始阶段,通过关键词检索feature,我脑子实在是记不住文档都在哪: 2.收尾阶段,检查 ...

  4. Python使用标准库zipfile+re提取docx文档中超链接文本和链接地址

    推荐教材: <Python程序设计实用教程>,ISBN:978-7-5635-6065-3,董付国,北京邮电大学出版社 教材封面: 全国各地新华书店有售 京东购买链接: 配套资源:教学大纲 ...

  5. html5多个图片位置_Python使用标准库zipfile提取docx文档中所有图片

    清华科技大讲堂免费直播课预告: 免费直播课|Python数据可视化与科学计算可视化案例分享,5月28日晚20:00-21:30============ 哔哩哔哩网站免费视频观看地址: 董老师在哔哩哔哩 ...

  6. Python使用标准库zipfile提取docx文档中所有图片

    清华科技大讲堂免费直播课预告: 免费直播课|Python数据可视化与科学计算可视化案例分享,5月28日晚20:00-21:30 ============ 哔哩哔哩网站免费视频观看地址: 董老师在哔哩哔 ...

  7. 在Word和PowerPoint 2007文档中嵌入True Type字体

    If you are the creative type that likes to use custom fonts in your documents or PowerPoint presenta ...

  8. Java web/springboot上传word/doc/docx文档(含图片)与HTML富文本导入/导出互相转换解析!附项目源码

    测试效果 先看下效果 文档内容如下: 上传 上传docx文档 查看解析内容 <html><head><style>p{margin-top:0pt;margin-b ...

  9. java 独立文档标签_怎么将带图片和表格的html标签转换为docx文档---java实现

    有人搞过将带图片和表格的html标签转换为docx文档吗?我转换为doc文档 wps打开不显示图片 office2016可以 2013不可以 现在想换为docx文档 有没有实现的?XDOC-Word文 ...

  10. docx文档怎么排列图片_“胶水语言”办公自动化Word篇——使用Python编辑和读取Word文档

    python调用word接口主要用到的模板为python-docx,基本操作官方文档有说明. 使用python新建一个word文档,操作就像文档里介绍的那样: from docx import Doc ...

最新文章

  1. 使用PHP将HTML + CSS转换为PDF? [关闭]
  2. react-native bundle --platform android --dev false --entry-file index.android.js --bundle-output and
  3. i3处理器_十代酷睿i3-10100F配GTX1650S组装机配置清单
  4. 转:WEB前端性能优化规则
  5. iOS中安全结束 子线程 的方法
  6. CSS基础学习-15-1.CSS 浏览器内核
  7. Linux下Socket网络编程
  8. html盒子自动居中,css盒子怎么居中?
  9. 2021 最新CV综述分类汇总(持续更新)
  10. [Elasticsearch] es 6.6 编译报错 java.net.ConnectException: Operation timed out
  11. Python 监控主机程序,异常后发送邮件(我的第一只Python程序)
  12. 课下测试ch17ch18
  13. 六石风格文档范例:做测试结果表格
  14. 网络盒子安装 linux,天猫魔盒安装原生linux的一些初步成果
  15. LordPE--计算RVA到Offset的值
  16. EasyPoi 模板导出Excel (带图片) 以及一些踩坑记录
  17. Chrome插件使用IE tab
  18. Error:java.lang.RuntimeException: Some file crunching failed
  19. Java网络五子棋程序设计论文
  20. 百度ueditor编辑器控制图片在编辑框中的大小

热门文章

  1. 20190926-win10电脑声音麦克风设置里没有增强属性怎么办听语音
  2. spring boot 搭建博客
  3. 美国白宫《国家人工智能研究发展战略计划》
  4. Feign的工作原理
  5. 我自己做了一个免费的百度网盘、BT、迅雷的加速器!
  6. Java web项目目录结构以及作用详解
  7. MATLAB编程 动态数组的创建
  8. 飞凌小课堂-RK3399开发板 linux双千兆网口方案-RTL8153
  9. Android 基础知识之 VelocityTracker使用(提供手指速度计算,可用于笔迹优化哦)
  10. 常见的DoS攻击防御方式