using (WordprocessingDocument wordDoc =
WordprocessingDocument.Open(document, true))
{
string docText = null;
using (StreamReader sr =
new StreamReader(wordDoc.MainDocumentPart.GetStream()))
{
docText = sr.ReadToEnd();
}
Regex regexText = new Regex("@@username@@");
docText = regexText.Replace(docText, "john thomas ");
using (StreamWriter sw =
new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
{
sw.Write(docText);
}
}
これにより、doctext の一致がコード内の名前に置き換えられます。doctext を確認したところ、置換する単語 ( @@username@@
) が分割されています。との間に XML コンテンツがある場合があり
@@
ますusername@@
。単語自体の形式が正しくない場合もあります。
を交換するにはどうすればよい@@username@@
ですか?