【问题标题】:Replacing data in word using DocumentFormat.OpenXml使用 DocumentFormat.OpenXml 替换 word 中的数据
【发布时间】:2018-11-14 17:20:22
【问题描述】:

我正在尝试使用 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();
        }
    }

请帮忙。

【问题讨论】:

    标签: c# openxml


    【解决方案1】:

    只是改变=>

    var sdtCont = body.InnerXml.Replace("Hello", "Hi");
    

    body.InnerXml=body.InnerXml.Replace("Hello", "Hi");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-08-30
      • 2020-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-05-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多