docX ReplaceText 工作不正确 [英] docX ReplaceText works incorrect

查看:30
本文介绍了docX ReplaceText 工作不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模板:

1.  Q1
 a. Q1a
 b. Q1b
 c. Q1c
 d. Q1d

...

  2.     Q10
    a.  Q10a
    b.  Q10b
    c.  Q10c
    d.  Q10d

我想用一些数据替换 Q1-Q10.我使用 DocX 库.

I want to replace Q1-Q10 with some data. I use DocX library.

  for (int q = 0; q < 20; q++)
        {
            docX.ReplaceText(String.Format("Q{0}", q+1), questions1[q].Text, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}a", q+1), questions1[q].AnswerA, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}b", q+1), questions1[q].AnswerB, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}c", q+1), questions1[q].AnswerC, false, RegexOptions.ExplicitCapture);
            docX.ReplaceText(String.Format("Q{0}d", q+1), questions1[q].AnswerD, false, RegexOptions.ExplicitCapture);
        }

所以输出应该是超过 9 个问题的:

So output should be for questions more then 9:

     2.  This is 10 question
     a. This is 10a question
     b. This is 10b question
     c. This is 10c question
     d. This is 10d question

但它就像:所以输出应该是超过 9 个问题的:

But it is like: So output should be for questions more then 9:

     2.  This is 1 question0
      a.    This is 1a question0
      b.    This is 1b question0
      c.    This is 1c question0
      d.    This is 1d question0

所以我假设 ReplaceText(src, dst) 搜索包含 src 的块并立即用 dst 替换.如何让它搜索 EXACT 值.

So I assume that ReplaceText(src, dst) searches for blocks that contains src and immidiatelly replaces with dst. How to make it search the EXACT value.

谢谢.

推荐答案

所以这个 DocX 库不能很好地完成这个任务,所以我使用了这个代码:

So This DocX library is not working fine fo this task, so I used this code:

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 matchWholeWord = true;//这一行很重要

object matchWholeWord = true; //this row is crutial

这篇关于docX ReplaceText 工作不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆