使用 DocumentFormat.OpenXml 替换 word 中的数据
Replacing data in word using DocumentFormat.OpenXml
我正在尝试使用 DocumentFormat.OpenXml 对 word 文档进行更改,我什至尝试查看以下问题:Save modified WordprocessingDocument to new file 但我找不到将更改保存到同一文件的方法。
尝试了以下但文件没有改变。
public static void WriteToWordDoc(string filepath)
{
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(filepath, true))
{
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
var old = body.InnerXml.ToString();
var sdtCont = body.InnerXml.Replace("Hello", "Hi");
wordprocessingDocument.MainDocumentPart.Document.Save();
wordprocessingDocument.Close();
}
}
请协助。
只是改变=>
var sdtCont = body.InnerXml.Replace("Hello", "Hi");
至
body.InnerXml=body.InnerXml.Replace("Hello", "Hi");
我正在尝试使用 DocumentFormat.OpenXml 对 word 文档进行更改,我什至尝试查看以下问题:Save modified WordprocessingDocument to new file 但我找不到将更改保存到同一文件的方法。
尝试了以下但文件没有改变。
public static void WriteToWordDoc(string filepath)
{
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(filepath, true))
{
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
var old = body.InnerXml.ToString();
var sdtCont = body.InnerXml.Replace("Hello", "Hi");
wordprocessingDocument.MainDocumentPart.Document.Save();
wordprocessingDocument.Close();
}
}
请协助。
只是改变=>
var sdtCont = body.InnerXml.Replace("Hello", "Hi");
至
body.InnerXml=body.InnerXml.Replace("Hello", "Hi");