C# 对Word文档进行处理

c#  对word文档进行处理 

public partial class Form1 : Form    {        public Microsoft.Office.Interop.Word.Document document;        public Microsoft.Office.Interop.Word.ApplicationClass wd = null;        public string filename = null;        private bool deactivateevents = false;        public int iWordID = 0;        public Form1()        {            InitializeComponent();        }        #region 加载文档        ///         ///加载需要处理的文档,备份,处理新生成的文档        ///         /// 需要处理的文件路径及名称,比如:C:\Users\jiankunking\Desktop\11.doc        /// 新文件名(包含路径),比如:C:\Users\jiankunking\Desktop\22.doc        public void WordHandle(string t_filename, string NewFileName)        {            deactivateevents = true;            filename = t_filename;            if (wd == null) wd = new Microsoft.Office.Interop.Word.ApplicationClass();            try            {                if (wd == null)                {                    //自定义处理                }                if (wd.Documents == null)                {                    //自定义处理                }                wd.CommandBars.AdaptiveMenus = false;                wd.DocumentBeforeClose += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentBeforeCloseEventHandler(OnClose);                wd.DocumentOpen += new Microsoft.Office.Interop.Word.ApplicationEvents4_DocumentOpenEventHandler(OnOpenDoc);                wd.ApplicationEvents4_Event_Quit += new Microsoft.Office.Interop.Word.ApplicationEvents4_QuitEventHandler(OnQuit);            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            if (document != null)            {                try                {                    object dummy = null;                    wd.Documents.Close(ref dummy, ref dummy, ref dummy);                }                catch (Exception ex)                {                    MessageBox.Show(ex.ToString());                }            }            object fileName = filename;            object newTemplate = false;            object docType = 0;            object isVisible = true;            object missing = System.Reflection.Missing.Value;            try            {                document = wd.Documents.Add(ref fileName, ref newTemplate, ref docType, ref isVisible);                if (document == null)                {                    //自定义处理                }                //这里获取进程信息,是为了关闭进程用的                //Process[] processes = Process.GetProcessesByName("WINWORD");                //iWordID = processes[processes.Length - 1].Id;                object newfileName = NewFileName;                document.SaveAs(ref newfileName, 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);                string sBKName = string.Empty;                         foreach (Microsoft.Office.Interop.Word.Bookmark bk in document.Bookmarks)                {                    sBKName = bk.Name;                    //word中的书签操作                }                document.Save();            }            catch (Exception ex)            {                MessageBox.Show(ex.ToString());            }            deactivateevents = false;        }        #endregion        ///         /// catches Word's close event         /// starts a Thread that send a ESC to the word window ;)        ///         ///         ///         private void OnClose(Microsoft.Office.Interop.Word.Document doc, ref bool cancel)        {            if (!deactivateevents)            {                cancel = true;            }        }        ///         /// catches Word's open event        /// just close        ///         ///         private void OnOpenDoc(Microsoft.Office.Interop.Word.Document doc)        {            OnNewDoc(doc);        }        ///         /// catches Word's newdocument event        /// just close        ///         ///         private void OnNewDoc(Microsoft.Office.Interop.Word._Document doc)        {            if (!deactivateevents)            {                deactivateevents = true;                object dummy = null;                doc.Close(ref dummy, ref dummy, ref dummy);                deactivateevents = false;            }        }        ///         /// catches Word's quit event        /// normally it should not fire, but just to be shure        /// safely release the internal Word Instance         ///         private void OnQuit()        {            //wd=null;        }    }

该代码段可以用来实现,打开某个Word文档,对其中的内容进行修改替换,比如:修改书签或者替换书签之类的。

 以上就是C#  对Word文档进行处理 的内容,更多相关内容请关注PHP中文网(www.php.cn)!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1432336.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 06:30:06
下一篇 2025年12月15日 19:53:57

相关推荐

  • C# 根据列名与列值设置当前行

    c# 根据列名与列值设置当前行 /// /// 根据GridView中某列的值与列名来设置当前行 /// /// 待设置当前行的GridView /// 某列的值 /// 列名 public void SetFocusedRow(DevExpress.XtraGrid.Views.Grid.Grid…

    好文分享 2025年12月17日
    000
  • C# 怎么检测string字符串中有哪些数字?

    c# 怎么检测string字符串中有哪些数字? //测试函数 private void Form1_Load(object sender, EventArgs e) { foreach (var number in ExtractNumbersFromString(“abc2345 345fdf67…

    好文分享 2025年12月17日
    000
  • C# where泛型约束

            最近无意中看到了:http://www.php.cn/。但是,人笨啊,木有看懂到底是啥意思,木办法自己写一个试试吧,权当做个笔记 例子如下: 接口: using System;using System.Collections.Generic;using System.Linq;usi…

    2025年12月17日
    000
  • C# 用Linq实现DataTable实现重复数据过滤

    未过滤前: 过滤后(仅会过滤掉完全一样的数据): 代码如下: DataTable dt = this.JsonToDataTable(“[{“Code”:”SortId”,”Name”:”SortId”},{“Code”:”SortCode”,”Name”:”编号”},{“Code”:”Soluti…

    2025年12月17日
    000
  • C# 参数带this是什么意思(扩展方法)

    扩展方法使你能够向现有类型“添加”方法,而无需创建新的派生类型、重新编译或以其他方式修改原始类型。 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样进行调用。 对于用 C# 和 Visual Basic 编写的客户端代码,调用扩展方法与调用在类型中实际定义的方法之间没有明显的差异。 最…

    2025年12月17日
    000
  • C# 枚举操作工具类

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;using System.ComponentModel;using System.Col…

    好文分享 2025年12月17日
    000
  • C# 如何识别一个字符串是否Json格式

    c# 如何识别一个字符串是否json格式 : using System;using System.Collections.Generic;using System.Text;namespace CYQ.Data.Tool{ /// /// 分隔Json字符串为字典集合。 /// internal c…

    好文分享 2025年12月17日
    000
  • C# 将 Json 解析成 DateTable

    c# 将 json 解析成 datetable  #region 将 Json 解析成 DateTable /// /// 将 Json 解析成 DateTable。 /// Json 数据格式如: /// {table:[{column1:1,column2:2,column3:3},{colum…

    2025年12月17日
    000
  • C# Excel数据验重及Table数据验重

    最近在做导入excel数据的时候,要检验数据是否重复:        1、要检验excel数据本身是否有重复?         2、Excel中的数据是否与数据库中的数据重复? 一、检验Excel中数据是否重复的方式有:        1、将Table中的数据使用select语句来过滤(此处略,可以…

    好文分享 2025年12月17日
    000
  • C# 读取Excel中的数据

    c# 读取excel中的数据 #region 读取Excel中的数据 /// /// 读取Excel中的数据 /// /// Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls /// Excel中的数据 private DataTable GetTable(st…

    好文分享 2025年12月17日
    000
  • C# 获取Excel工作薄中Sheet页(工作表)名集合

    c#  获取excel工作薄中sheet页(工作表)名集合 #region 获取Excel工作薄中Sheet页(工作表)名集合 /// /// 获取Excel工作薄中Sheet页(工作表)名集合 /// /// Excel文件名及路径,EG:C:\Users\JK\Desktop\导入测试.xls …

    好文分享 2025年12月17日
    000
  • C# Timer 定时器应用

            关于c#中timer类 在c#里关于定时器类就有3个:        1.定义在system.windows.forms里        2.定义在system.threading.timer类里        3.定义在system.timers.timer类里        sy…

    2025年12月17日
    000
  • C# 动态加载Dll

    1、新建测试dll及方法,用vs2010新建winform程序,具体代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawi…

    好文分享 2025年12月17日
    000
  • C# 实体类序列化与反序列化一 (XmlSerializer)

    /// /// 实体类序列化的反序列化的类 /// /// public abstract class EntityClassXmlSerializer { /// /// 实体类序列化成xml string /// /// /// public static string ToXMLString(…

    好文分享 2025年12月17日
    000
  • C# Json 序列化与反序列化一

    public class JsonSerializer { /// /// json序列化 /// /// /// /// public static string JsonStringSerializer(T t) { DataContractJsonSerializer ser = new Da…

    好文分享 2025年12月17日
    000
  • C# 容器上控件排序

    public static class Sort { #region 设置PanelControl上按钮显示位置 /// /// 设置按钮显示位置 /// /// 需要调整按钮顺序的Panel /// 按钮间隔 public static void SetButtonCenter(Scrollabl…

    好文分享 2025年12月17日
    000
  • C# 移除数组中重复数据

    #region 移除数组中重复数据 /// /// 移除数组中重复数据 /// /// 需要除重的数组 /// 不重复数组 public static string[] DelRepeatData(string[] array) { return array.GroupBy(p => p).S…

    好文分享 2025年12月17日
    000
  • C# 字符串中多个连续空格转为一个空格

    #region 字符串中多个连续空格转为一个空格 /// /// 字符串中多个连续空格转为一个空格 /// /// 待处理的字符串 /// 合并空格后的字符串 public static string MergeSpace(string str) { if (str != string.Empty …

    好文分享 2025年12月17日
    000
  • C# 多态性

    转载自:MSDN 类似文章:点击打开链接  Polymorphism(多态性)是一个希腊词,指“多种形态”,多态性具有两个截然不同的方面:  发生此情况时,该对象的声明类型不再与运行时类型相同。  在运行时,客户端代码调用该方法,CLR 查找对象的运行时类型,并调用虚方法的重写方法。 因此,你可以在…

    2025年12月17日
    000
  • C# 自定义 implicit和explicit转换

    explicit 和 implicit 属于转换运算符,如用这两者可以让我们自定义的类型支持相互交换explicti 表示显式转换,如从 a -> b 必须进行强制类型转换(b = (b)a)implicit 表示隐式转换,如从 b -> a 只需直接赋值(a = b) 隐式转换可以让我…

    2025年12月17日
    000

发表回复

登录后才能评论
关注微信