簡體   English   中英

c# word interop 查找並替換所有內容

[英]c# word interop find and replace everything

我有一些代碼來替換 word 2010 docx 中的文本。

        object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "document.docx");

        Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };

        Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(ref fileName, ReadOnly: false, Visible: true);

        aDoc.Activate();

        Microsoft.Office.Interop.Word.Find fnd = wordApp.ActiveWindow.Selection.Find;

        fnd.ClearFormatting();
        fnd.Replacement.ClearFormatting();
        fnd.Forward = true;

        fnd.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;

        fnd.Text = "{id}";
        fnd.Replacement.Text = "123456";
        fnd.Execute(Replace: WdReplace.wdReplaceAll);

這無需格式化即可工作。 但是當 {id} 被格式化時,它不會替換文本。

如何使此代碼忽略格式?

我用這個函數來查找和替換。 您可以指定任何選項。

private void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, object findText, object replaceWithText)
{
    //options
    object matchCase = false;
    object matchWholeWord = true;
    object matchWildCards = false;
    object matchSoundsLike = false;
    object matchAllWordForms = false;
    object forward = true;
    object format = false;
    object matchKashida = false;
    object matchDiacritics = false;
    object matchAlefHamza = false;
    object matchControl = false;
    object read_only = false;
    object visible = true;
    object replace = 2;
    object wrap = 1;
    //execute find and replace
    doc.Selection.Find.Execute(ref findText, ref matchCase, ref matchWholeWord,
        ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, ref replaceWithText, ref replace,
        ref matchKashida ,ref matchDiacritics, ref matchAlefHamza, ref matchControl);                
}

用法是:

object fileName = Path.Combine(System.Windows.Forms.Application.StartupPath, "document.docx");
Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application { Visible = true };
Microsoft.Office.Interop.Word.Document aDoc = wordApp.Documents.Open(fileName, ReadOnly: false, Visible: true);
aDoc.Activate();
FindAndReplace(wordApp, "{id}", "12345");

您可以一遍又一遍地使用 FindAndReplace 函數....
希望這會有所幫助。

從 Visual Studio 2013 你可以這樣做:

Microsoft.Office.Interop.Word.Range range = this.Application.ActiveDocument.Content;
range.Find.ClearFormatting();
range.Find.Execute(FindText: "find text", ReplaceWith: "replace text", Replace: Word.WdReplace.wdReplaceAll);

(為了像我這樣遇到這個問題但不一定使用與 OP 相同版本的工具的人的利益而發布。)

如果字符串包含超過 255 個字符,則該方法將字符串分割。

    void FindAndReplace(Microsoft.Office.Interop.Word.Application doc, string findText, string replaceWithText)
    {
        if (replaceWithText.Length > 255)
        {
            FindAndReplace(doc, findText, findText + replaceWithText.Substring(255));
            replaceWithText = replaceWithText.Substring(0, 255);
        }

        //options
        object matchCase = false;
        object matchWholeWord = true;
        object matchWildCards = false;
        object matchSoundsLike = false;
        object matchAllWordForms = false;
        object forward = true;
        object format = false;
        object matchKashida = false;
        object matchDiacritics = false;
        object matchAlefHamza = false;
        object matchControl = false;
        object read_only = false;
        object visible = true;
        object replace = 2;
        object wrap = 1;

        //execute find and replace
        doc.Selection.Find.Execute(findText, ref matchCase, ref matchWholeWord,
            ref matchWildCards, ref matchSoundsLike, ref matchAllWordForms, ref forward, ref wrap, ref format, replaceWithText, ref replace,
            ref matchKashida, ref matchDiacritics, ref matchAlefHamza, ref matchControl);
    }

你可以試試這個:

var doc =  new Microsoft.Office.Interop.Word.Application().Documents.Open("document.docx");

doc.Content.Find.Execute( "{id}", false, true, false, false, false, true, 1, false,  "12345", 2,
false, false, false, false);
doc.Save();

如果有人在 2021 年尋找答案並使用 VS2019,您可以使用此方法,包括替換文本框等形狀內的文本。

 private void FindAndReplace(Application app,Document doc, string findText, string replaceWithText)
        {
            Find findObject = app.Selection.Find;
            findObject.ClearFormatting();
            findObject.Text = findText;
            findObject.Replacement.ClearFormatting();
            findObject.Replacement.Text = replaceWithText;
            object missing = System.Reflection.Missing.Value;
            object replaceAll = WdReplace.wdReplaceAll;
            findObject.Execute(ref missing, ref missing, ref missing, ref missing, ref missing,ref missing, ref missing, ref missing, ref missing, ref missing, ref replaceAll, ref missing, ref missing, ref missing, ref missing);

            var shapes = doc.Shapes;
            foreach (Shape shape in shapes)
            {
                if (shape.TextFrame.HasText != 0)
                {
                    var initialText = shape.TextFrame.TextRange.Text;
                    var resultingText = initialText.Replace(findText, replaceWithText);
                    if (initialText != resultingText)
                    {
                        shape.TextFrame.TextRange.Text = resultingText;
                    }
                }
            }
        }

我創建了一個庫,您可以在其中將占位符放在word docx文檔中。 您也可以轉換為PDF等。如果您只想用文本,表格行(我內置“ngrepeat”)或圖像替換占位符,則不需要LibreOffice。 您可以在此處找到詳細說明和示例項目:

https://github.com/smartinmedia/Net-Core-DocX-HTML-To-PDF-Converter

它可以執行這些轉換(以及替換占位符的模板/報表引擎):

  • DOCX到DOCX
  • DOCX到PDF
  • DOCX到HTML
  • HTML到HTML
  • HTML到PDF
  • HTML到DOCX

您可以添加占位符:

  • 文本
  • 重復表行
  • 圖片

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM