C# 替換Word文字【包含頁首、頁尾、文字框、普通文字的替換】

來源:互聯網
上載者:User

標籤:c# word替換   c# word替換頁首文字   c# 替換頁尾文字   c#替換文字框文字   c# word替換文本   

---使用

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ToWord
{
    class Program
    {
        static void Main(string[] args)
        {
            string sourceFile = @"c:\users\administrator\documents\visual studio 2012\Projects\ToWord\ToWord\WordModel.doc";
            string destFile = @"c:\users\administrator\documents\visual studio 2012\Projects\ToWord\ToWord\Model\WordModel.doc";
            //複製檔案
            System.IO.File.Copy(sourceFile, destFile, true);
            WordOperate wordOperate = new WordOperate();

            wordOperate.Open(destFile);
            wordOperate.Replace("[項目名標籤]","神沒什麼項目的");
            wordOperate.Replace("[項目編號簽]", "神沒什麼項目的");
            wordOperate.Save();
        }
    }
}

 

 

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;

namespace ToWord
{
    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();
            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);
        }
    }
}


 

 

C# 替換Word文字【包含頁首、頁尾、文字框、普通文字的替換】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.