简体   繁体   中英

OpenXML - replace a string by a table

I want to replace a string by a table, I can't make it work.

Here is the code for replacing a string:

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();
}

What should I do?

Can you show your regex? ( findeDas value).

Also you could probably try to use other override of Replace:

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();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM