【问题标题】:OpenXML - replace a string by a tableOpenXML - 用表格替换字符串
【发布时间】:2020-06-30 03:41:45
【问题描述】:

我想用表格替换一个字符串,我做不到。

下面是替换字符串的代码:

var newDoc = new FileInfo(OpenXml_docx);
File.Copy(sourceDoc.FullName, newDoc.FullName);
using (WordprocessingDocument wDoc = WordprocessingDocument.Open(newDoc.FullName, true)) {
    XDocument xDoc = wDoc.MainDocumentPart.GetXDocument();
    IEnumerable<XElement> content = xDoc.Descendants(W.p);
    int count = OpenXmlRegex.Replace(content, findeDas, "#BlaBla", null);
    Console.WriteLine("Example #1 Count: {0}", count); 
    wDoc.MainDocumentPart.PutXDocument();
}

我该怎么办?

【问题讨论】:

    标签: c# openxml openxml-sdk openxml-powertools


    【解决方案1】:

    你能展示你的正则表达式吗? (findeDas 值)。

    您也可以尝试使用其他替代替换:

    var newDoc = new FileInfo(OpenXml_docx);
    File.Copy(sourceDoc.FullName, newDoc.FullName);
    using (WordprocessingDocument wDoc = WordprocessingDocument.Open(newDoc.FullName, true)) {
        XDocument xDoc = wDoc.MainDocumentPart.GetXDocument();
        IEnumerable<XElement> content = xDoc.Descendants(W.p);
        int count = 0;
        foreach (var e in content) {  
            count += OpenXmlRegex.Replace(e, findeDas, "#BlaBla", null);
        }
        Console.WriteLine("Example #1 Count: {0}", count); 
        wDoc.MainDocumentPart.PutXDocument();
    }
    

    【讨论】:

    • 这是正则表达式(@"\${(.*?)}";),它将应用于${somePlaceholderInWord}这种类型的所有占位符。
    • 你能举出你的Word的例子吗?因为它适用于具有1 ${aaa} ewq 等内容的简单文档。 {} 内是否有换行符?
    • {} 内部没有换行符,但它们位于表格、段落中的不同位置...
    猜你喜欢
    • 1970-01-01
    • 2020-12-19
    • 1970-01-01
    • 2021-05-11
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    相关资源
    最近更新 更多