【问题标题】:Using OpenXML SDK to replace text on a docx file with a line break (newline)使用 OpenXML SDK 将 docx 文件中的文本替换为换行符(换行符)
【发布时间】:2014-10-10 20:37:43
【问题描述】:

我正在尝试使用 C# 将 整个 DOCX 文件中的特定文本字符串替换为换行符(换行符)。

我正在搜索的文本字符串可能位于文件的段落或表格中。

我目前正在使用下面的代码来替换文本。

using (WordprocessingDocument doc = WordprocessingDocument.Open("yourdoc.docx", true))
{
  var body = doc.MainDocumentPart.Document.Body;

  foreach (var text in body.Descendants<Text>())
  {
    if (text.Text.Contains("##Text1##"))
    {
      text.Text = text.Text.Replace("##Text1##", Environment.NewLine);
    }
  }
}

问题:当我运行此代码时,输​​出 DOCX 文件的文本被替换为空格(即“”)而不是换行符。

如何更改此代码以使其正常工作?

【问题讨论】:

    标签: c# ms-word openxml docx openxml-sdk


    【解决方案1】:

    尝试使用break。检查此链接上的示例。你只需要附加一个 Break

    段落、智能标签、超链接都在Run 内。所以也许你可以试试这个approach。 要更改表格中的文本,您必须使用此approach。同样,文本始终在 Run 中。

    如果你说替换只是替换一个空字符串,我会试试这个:

    using (WordprocessingDocument doc =
                    WordprocessingDocument.Open(@"yourpath\testdocument.docx", true))
            {
                var body = doc.MainDocumentPart.Document.Body;
                var paras = body.Elements<Paragraph>();
    
                foreach (var para in paras)
                {
                    foreach (var run in para.Elements<Run>())
                    {
                        foreach (var text in run.Elements<Text>())
                        {
                            if (text.Text.Contains("text-to-replace"))
                            {
                                text.Text = text.Text.Replace("text-to-replace", "");
                run.AppendChild(new Break());
                            }
                        }
                    }
                }
            }
    

    【讨论】:

    • 感谢您对此进行调查。使用run.AppendChild(new Break()); 可能会创建一个新行。但是我怎样才能把它只放在应该替换文本的地方呢?
    • Text.Replace() 函数我相信只接受两个字符串
    • 这只替换单行文本。文本可以在多个运行中拆分。
    【解决方案2】:

    不要添加换行符,而是尝试制作两个段落,一个在“要替换的文本”之前,一个在之后。这样做会自动在两个段落之间添加换行符。

    【讨论】:

      【解决方案3】:

      text.Parent.Append(new DocumentFormat.OpenXml.Wordprocessing.Break());

      【讨论】:

      • 嗨,阿伦,欢迎来到 SO。您能否编辑此答案以解释为什么此代码可以解决问题以及它是如何工作的?此处不鼓励仅使用代码回答。
      【解决方案4】:
      // Open a WordprocessingDocument for editing using the filepath.
      using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
      {
       // Assign a reference to the existing document body.
       Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
      
       //itenerate throught text
       foreach (var text in body.Descendants<Text>())
       {
         text.Parent.Append(new Text("Text:"));
         text.Parent.Append(new Break());
         text.Parent.Append(new Text("Text"));
       }
      }
      

      这将返回:

      文字:
      文字:

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-26
        • 1970-01-01
        相关资源
        最近更新 更多