• WordOperate


    using System;
    using System.Collections.Generic;
    using System.Diagnostics;
    using System.Linq;
    using System.Text;
    namespace Common
    {
        public class WordOperate : IDisposable
        {
            private Microsoft.Office.Interop.Word._Application _app;
            private Microsoft.Office.Interop.Word._Document _doc;
            object _nullobj = System.Reflection.Missing.Value;
            /// <summary>
            /// 关闭Word进程
            /// </summary>
            public void KillWinword()
            {
                var p = Process.GetProcessesByName("WINWORD");
                if (p.Any()) p[0].Kill();
            }
            /// <summary>
            /// 打开word文档
            /// </summary>
            /// <param name="filePath"></param>
            public void Open(string filePath)
            {
                //_app = new Microsoft.Office.Interop.Word.ApplicationClass();//原版
                _app = new Microsoft.Office.Interop.Word.Application();
                object file = filePath;
                _doc = _app.Documents.Open(
                     ref file, ref _nullobj, ref _nullobj,
                     ref _nullobj, ref _nullobj, ref _nullobj,
                     ref _nullobj, ref _nullobj, ref _nullobj,
                     ref _nullobj, ref _nullobj, ref _nullobj,
                     ref _nullobj, ref _nullobj, ref _nullobj, ref _nullobj);
            }
    
            /// <summary>
            /// 替换word中的文字
            /// </summary>
            /// <param name="strOld">查找的文字</param>
            /// <param name="strNew">替换的文字</param>
            public void Replace(string strOld, string strNew)
            {
                //替换全局Document
                _app.Selection.Find.ClearFormatting();
                _app.Selection.Find.Replacement.ClearFormatting();
                _app.Selection.Find.Text = strOld;
                _app.Selection.Find.Replacement.Text = strNew;
                object objReplace = Microsoft.Office.Interop.Word.WdReplace.wdReplaceAll;
                _app.Selection.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref objReplace, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj);
                //替换页脚的字
                foreach (Microsoft.Office.Interop.Word.Section wordSection in _doc.Sections)
                {
                    Microsoft.Office.Interop.Word.Range footerRange = wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    footerRange.Find.ClearFormatting();
                    footerRange.Find.Replacement.ClearFormatting();
                    footerRange.Find.Text = strOld;
                    footerRange.Find.Replacement.Text = strNew;
                    footerRange.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref objReplace, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj);
                }
                //替换页眉的字
                foreach (Microsoft.Office.Interop.Word.Section section in _doc.Sections)
                {
                    Microsoft.Office.Interop.Word.Range headerRange = section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
                    headerRange.Find.ClearFormatting();
                    headerRange.Find.Replacement.ClearFormatting();
                    headerRange.Find.Text = strOld;
                    headerRange.Find.Replacement.Text = strNew;
                    headerRange.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj,
                                           ref _nullobj, ref objReplace, ref _nullobj,
                                           ref _nullobj, ref _nullobj, ref _nullobj);
                }
                //文本框
                Microsoft.Office.Interop.Word.StoryRanges storyRanges = _doc.StoryRanges;
                foreach (Microsoft.Office.Interop.Word.Range range in storyRanges)
                {
                    Microsoft.Office.Interop.Word.Range rangeFlag = range;
                    if (Microsoft.Office.Interop.Word.WdStoryType.wdTextFrameStory == rangeFlag.StoryType)
                    {
                        while (rangeFlag != null)
                        {
                            rangeFlag.Find.ClearFormatting();
                            rangeFlag.Find.Replacement.ClearFormatting();
                            rangeFlag.Find.Text = strOld;
                            rangeFlag.Find.Replacement.Text = strNew;
                            rangeFlag.Find.Execute(ref _nullobj, ref _nullobj, ref _nullobj,
                                                   ref _nullobj, ref _nullobj, ref _nullobj,
                                                   ref _nullobj, ref _nullobj, ref _nullobj,
                                                   ref _nullobj, ref objReplace, ref _nullobj,
                                                   ref _nullobj, ref _nullobj, ref _nullobj);
                            rangeFlag = range.NextStoryRange;
                        }
                    }
                }
    
            }
            /// <summary>
            /// 保存
            /// </summary>
            public void Save(bool disposet = true)
            {
                if (disposet == false)
                {
                    _doc.Save();
                }
                else
                {
                    this.Save(false);
                    this.Dispose();
                    this.KillWinword();
                }
            }
            /// <summary>
            /// 退出
            /// </summary>
            public void Dispose()
            {
                _doc.Close(ref _nullobj, ref _nullobj, ref _nullobj);
                _app.Quit(ref _nullobj, ref _nullobj, ref _nullobj);
            }
        }
    }
  • 相关阅读:
    JavaScript初学者应注意的七个细节
    KindEditor 编辑器使用方法
    有关 JavaScript 的 10 件让人费解的事情
    能说明你的Javascript技术很烂的五个原因
    分享10个便利的HTML5/CSS3框架
    现在就使用HTML5的十大原因
    你应该知道的Node.js扩展模块——Hashish
    C++ Tip: How To Get Array Length | Dev102.com
    MPI for Python — MPI for Python v1.3 documentation
    http://construct.readthedocs.org/en/latest/basics.html
  • 原文地址:https://www.cnblogs.com/lee2011/p/6113501.html
Copyright © 2020-2023  润新知