使用COM组件读写word文档
2013-05-14 18:21
489 查看
引用组件: 在菜单栏选择“项目”-“添加引用”,弹出的窗口中我们可以选择“COM”选项卡,找到Microsoft Office 12.0 Object Library(Office 2003/2007需要使用12.0版的,如果你使用的是Office 2000或者更低的版本,只要载入10.0版的就可以了),确定后引入 先看一下Word对像模型 Application :用来表现WORD应用程序,包含其它所有对象。他的成员经常应用于整个WORD,你可以用它的属性和方法控制WORD环境。 Document :Document对象是WORD编程的核心。当你打开一个已有的文档或创建一个新的文档时,就创建了一个新的Document对象, 新创建的Document将会被添加到Word Documents Collection。 Selection :Selection对象是描述当前选中的区域。若选择区域为空,则认为是当前光标处。 Rang :是Document的连续部分,根据起始字符和结束字符定义位置。 Bookmark:类似于Rang,但Bookmark可以有名字并在保存Document时Bookmark也被保存 protected void EditFile(string Source) //修改文件 { Source = Server.MapPath(Source); //定义打开文件的16个参数 object fileName = Source; //文件名称 object ConfirmConversions = false; //允许转换 object ReadOnly = false; //只读方式打开 object AddToRecentFiles = false; //添加到最近打开的文档 object PasswordDocument = System.Type.Missing; object PasswordTemplate = System.Type.Missing; object Revert = System.Type.Missing; object WritePasswordDocument = System.Type.Missing; object WritePasswordTemplate = System.Type.Missing; object Format = System.Type.Missing; //格式 object Encoding = System.Type.Missing; //编码 object Visible = System.Type.Missing; object OpenAndRepair = System.Type.Missing; object DocumentDirection = System.Type.Missing; object NoEncodingDialog = System.Type.Missing; object XMLTransform = System.Type.Missing; Object Nothing = System.Reflection.Missing.Value; Word.Application wordApp = new Word.Application(); 实例化一个word application对象 Word.Document opendoc = wordApp.Documents.Open(ref fileName, ref ConfirmConversions, ref ReadOnly, ref AddToRecentFiles, ref PasswordDocument, ref PasswordTemplate, ref Revert, ref WritePasswordDocument, ref WritePasswordTemplate,ref Format, ref Encoding, ref Visible, ref OpenAndRepair, ref DocumentDirection, ref NoEncodingDialog, ref XMLTransform);打开一个word文档 opendoc.Activate(); //激活文档,使文档为当前处理 string str=opendoc.Content.Text; //读出word文档 opendoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭文档对象 str = str.Replace("<br>",""); Word.Document Newdoc = wordApp.Documents.Add(ref Nothing, ref Nothing,ref Nothing, ref Nothing); //创建一个word文档 Newdoc.Content.Text = str; //写入word文档 Newdoc.SaveAs(ref fileName, ref Format, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing, ref Nothing); //关闭wordDoc文档对象 Newdoc.Close(ref Nothing, ref Nothing, ref Nothing); //关闭wordApp组件对象 wordApp.Quit(ref Nothing, ref Nothing, ref Nothing); }
相关文章推荐
- PHP使用COM组件转换word文档为HTML并实现在线预览
- 使用OpenXml SDK 给Word文档添加样式并应用于文本
- 使用Velocity导出Word文档
- 使用Word 2007快速将多个文档合而为一
- 使用WPS打开的word文档内容复制不到excel里
- 使用 dsoframer.ocx 实现在线编辑 word Access 等文档
- 如何嵌入并自动使用 MFC 的 Word 文档
- Python-docx:读写word文档的Python库
- 【10】python 读写word文档
- 本文演示如何创建和使用自动化操作从 Visual c + +.net 和 Microsoft 基础类 (MFC) Word 文档
- java使用POI工具读取word文档并写入生成新的word文档及错误整理
- 使用PHP创建word文档(平台无关)
- LyX使用小记之四 word文档中的内容插入LyX
- 使用java将多种类型的文件如Word、PDF、JPG汇总到一个文档中(Word或者PDF)
- word文档中如何使用题注
- 使用 Word 2007 以从右向左书写文档,在Word 2002 或 Word 2000 中打开顺序颠倒
- C# 中使用Word文档对图像进行操作
- 使用C#自动生成Word2003文档(通过操作COM组件实现) [转]
- 如何使用C#向Word文档中添加表格和嵌套表格
- 使用Apache POI读写Microsoft Office文档之Excel(一)