C#操作 word代码

#region  读取word
        /// <summary>
        /// 读取word所有文字内容(不包含表格)
        /// </summary>
        /// <returns>word中的字符内容(纯文本)</returns>
        public string ReadAllFromWord()
        {
            Word.ApplicationClass app = null;
            Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
            object readOnly = true;
            object isVisible = false;
            try
            {
                app = new Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);

                string textString = "";
                //读取全部内容 何问起 hovertree.com
                textString = doc.Content.Text.Trim();
//                int ParCount = this.getParCount(doc);//段数
//                for (int i = 1 ; i <= ParCount ; i++)
//                {
//                    textString = textString + doc.Paragraphs[i].Range.Text.Trim();//doc.Content.Text.Trim();//
//                }
                textString = textString.Replace("/a","");    //替换空串为空。(word中/a代表空串,但在C#中,代表响铃 晕~~)否则显示控制台程序时会响
                textString = textString.Replace("/r","/n");    //替换回车为回车换行
                return textString;
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (doc != null)
                {
                    try
                    {
                        doc.Close(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    doc = null;
                }
                if (app != null)
                {
                    try
                    {
                        app.Quit(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();

            }
        }
        #endregion

#region 追加写入word /// <summary>
        /// 追加写入word
        /// </summary>
        /// <param name="InsertText">需要写入的字符串</param>
        public void WriteToWord(string InsertText)
        {
            Word.ApplicationClass app = null;
            Word.Document doc = null;
            object missing = System.Reflection.Missing.Value;
            object FileName = m_FilePath;//@"E:/学习试验项目/ReadFromWordDoc/test.doc";
            object readOnly = false;
            object isVisible = false;
            try
            {
                app = new Word.ApplicationClass();
                doc = app.Documents.Open(ref FileName, ref missing, ref readOnly,
                    ref missing, ref missing, ref missing, ref missing, ref missing,
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,
                    ref missing, ref missing, ref missing);
                //激活word文档
                doc.Activate();
                //追加到最后一段(段落是按照 /n 来作为标志的)
                doc.Paragraphs.Last.Range.Text = InsertText + "/n";//加个结束符(增加一段),否则再次插入的时候就成了替换.
                //保存
                doc.Save();
            }
            catch(Exception ex)
            {
                throw ex;
            }
            finally
            {
                if (doc != null)
                {
                    try
                    {
                        doc.Close(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    doc = null;
                }
                if (app != null)
                {
                    try
                    {
                        app.Quit(ref missing, ref missing, ref missing);
                    }
                    catch
                    {}
                    app = null;
                }
                GC.Collect();
                GC.WaitForPendingFinalizers();
            }
        }

        #endregion

推荐:http://www.cnblogs.com/roucheng/p/3521864.html

时间: 2024-07-29 02:47:53

C#操作 word代码的相关文章

转载:C# Word操作实现代码

转载自:http://www.jb51.net/article/17770.htm 在VS2008平台下,引用.net-Microsoft.Office.Interop.Word.12,这样就可以在程序用操作WORD对象了. 通过简单执行,报了80070005错误,这个错误是因为权限不够,需要在DCOM配置中更改.net和IIS用户的操作权限,具体修改过程如下: 解决方法一: 1.控制面板->管理工具->组件服务->计算机->我的电脑->DCom配置->找到Micros

C#操作Word的超详细总结

本文中用C#来操作Word,包括: 创建Word: 插入文字,选择文字,编辑文字的字号.粗细.颜色.下划线等: 设置段落的首行缩进.行距: 设置页面页边距和纸张大小: 设置页眉.页码: 插入图片,设置图片宽高以及给图片添加标题: 插入表格,格式化表格,往表格中插入数据: 保存Word,打印Word: 重新打开Word等. Visual studio版本:Visual Studio 2012(2010应该也可以) 准备工作: /* 1. 添加引用COM里面的 Microsoft Word 12.0

用C#操作word替换字符,不用npoi,改用spire

这两天想写个小程序,是用C#操作word文档的.许多人都对微软本身的解决方案COM组件十分不看好,比如需要本机安装office等等,总之吐槽很多,直接放弃. 搜到一个国产的npoi库,据说操作简单功能强大,下载试用,发现操作excel还是不错的,但word不好使.而且官方网站文档不全,更新缓慢. 尝试文本替换,总是出错.加了官方群,问了问题,没人回应. 网上又找了找,发现有个spire的库不错,也有免费的dll可以用.(转个评价:这是一个免费又强大的C# word 组件,它不需要 Word au

C#中操作Word(1)—— word对象模型介绍

一.开发环境布置 C#中添加对Word的支持,只需添加对Microsoft.Office.Interop.Word的命名空间,如下图所示,右键点击“引用”,在弹出的“添加引用”对话框中选中COM标签页,找到“Microsoft Word 12.0 Object Library”. 点击确定按钮后,可在引用中添加显示名称为Microsoft.Office.Interop.Word的引用: 二.Word的对象模型介绍 Word中共有5种常用的对象模型:应用程序对象Application.文档对象Do

XDocReport 的简单使用 操作word 替换变量

XDocReport 主要是操作word,在word模版中定义变量并替换变量.(在word中还可替换动态图片,可进行循环.判断操作,可定义指令扩展程序,可转成pdf文件 等) 1,模版变量定义. 新建word,Ctrl + F9   编辑域   选择MergeField  编辑域代码 如图: 2,代码 /** * 根据模板导出word文件 * * @param reportData ReportData对象为数据对象,里面存储Map 数据 * @param templateName 模板文件路径

C#操作Word文档(加密、解密、对应书签插入分页符)

原文:C#操作Word文档(加密.解密.对应书签插入分页符) 最近做一个项目,客户要求对已经生成好的RTF文件中的内容进行分页显示,由于之前对这方面没有什么了解,后来在网上也找了相关的资料,并结合自己在MSDN上面的查找,后来总算把问题给解决掉啦.下面对C#操作Word文档(加密.解密.插入分页符)做一个简单的总结,希望对一些朋友有所帮忙吧.^_^ 写代码之前,需要引用对应的DLL文件: 1.Interop.Microsoft.Office.Interop.Word.dll  (网上可以下载)

[.NET] 操作 Word 组件 - Spire.Doc 介绍

操作 Word 组件 - Spire.Doc 介绍 [博主]反骨仔 [原文地址]http://www.cnblogs.com/liqingwen/p/5898368.html 序 本打算过几天简单介绍下组件 Spire.XLS,突然发现园友率先发布了一篇,既然 xls 已经出现,为避免打上抄袭嫌疑,博主只能抢先一步使用 Spire.Doc 简单介绍 Doc 操作,下面是通过 WinForm 程序执行代码完成介绍的. 本机环境:Win10 x64.VS 2015.MS Office 2016. 目

[转] c# 操作Word

来自 风过四季天 的原文 c# 操作Word总结 在医疗管理系统中为保存患者的体检和治疗记录,方便以后的医生或其他人查看.当把数据保存到数据库中,需要新建很多的字段,而且操作很繁琐,于是想 到网页的信息创建到一个word文本中,在显示的时,可以在线打开word,也可以把word转换成html标签显示. 这样使用word代替网页的原因有: 第一:网页生成数学公式和特殊符号存储和显示比较麻烦(如何操作word生成数学公式,有待测试) 第二:生成Word版的报告更容易存档和没有环境下的传阅及打印 第三

●操作Word

前提:电脑上需要安装Office 第一步:添加引用.Microsoft.Word.xxx.Object.Library 注意:把引用中的Microsoft.Office.Interop.Word的属性中的“嵌入互操作”设为false.否则上面的代码会报错. 第二步:导入命名空间.using MSWord = Microsoft.Office.Interop.Word;//MCWord可以用来替代Microsoft.Office.Interop.Word 第三步:创建Word应用程序. MSWor