我正在使用 Open XML Office SDK 2.0 在 word 文档中搜索字符串并列出这些字符串。
MatchCollection Matches;
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(txtLocation.Text, true))
{
string docText = null;
using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regex = new Regex(@"\(.*?\)");
Matches = regex.Matches(docText);
}
int i = 0;
while (i < Matches.Count)
{ Label lb = new Label();
lb.Text = Matches[i].ToString();
lb.Location = new System.Drawing.Point(24, (28 + i * 24));
this.panel1.Controls.Add(lb);
i++;
}
问题是有时它返回正确的字符串,例如: (HelloWorld) 但有时它与标签完全不同,例如: < w:rFonts w:ascii="Arial" w:hAnsi="Arial" w:cs="Arial "/ >
我该如何摆脱这些?