docX ReplaceText工作不正确
本文关键字:不正确 工作 ReplaceText docX | 更新日期: 2023-09-27 18:18:25
我有一个模板:
1. Q1
a. Q1a
b. Q1b
c. Q1c
d. Q1d
。..
2. Q10
a. Q10a
b. Q10b
c. Q10c
d. Q10d
我想用一些数据代替Q1-Q10。我使用DocX库。
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的问题:
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的问题:
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。如何使它搜索精确值。
谢谢。
所以这个DocX库不能很好地完成这个任务,所以我使用了这个代码:
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);
}
objectmatchwholeword = true;//这一行很重要