Search And Replace Text in OPENXML (Added file)

假如想象 提交于 2019-12-25 03:34:38

问题


I know there is alot of posts on it, BUT nothing worked for my problem: Im using OPENxml to create word document, and I am adding some ready files to the document during the creation. I want to change some text in the file that I am adding after the document is ready. So thats what I tried: First creating the document:

fileName = HttpContext.Current.Server.MapPath("~/reports/"+fileName+".docx");
using (var doc = WordprocessingDocument.Create(
    fileName, WordprocessingDocumentType.Document))
{
    ///add files and content inside the document
    addContentFile("template1part1", HttpContext.Current.Server.MapPath("~/templates/template1part1.docx"), mainPart);
}

this is how I am adding the files:

private static void addContentFile(string id,string path, MainDocumentPart mainPart){
    string altChunkId = id;
    AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(
        AlternativeFormatImportPartType.WordprocessingML, altChunkId);
    using (FileStream fileStream = File.Open(path, FileMode.Open))
    {
        chunk.FeedData(fileStream);
        fileStream.Close();
    }
    AltChunk altChunk = new AltChunk();
    altChunk.Id = altChunkId;
    mainPart.Document.Body.Append(altChunk);
    mainPart.Document.Save();
}

And this is how I am trying to replace text AFTER I created the file (after i finished to use WordprocessingDocument)

First try:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
{
    string docText = null;
    using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        docText = sr.ReadToEnd();

    docText = new Regex(findText, RegexOptions.IgnoreCase).Replace(docText, replaceText);

    using (StreamWriter sw = new StreamWriter(wordDoc.MainDocumentPart.GetStream(FileMode.Create)))
        sw.Write(docText);
}

Second try:

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", "replaced-text");
                    }
                }
            }
        }
    }
}

None of them worked, and I tried much more. Its worked for text that I am manually add to the document, but its now working for text that I am adding from the ready files. there is a way to do it?


回答1:


The way you are adding the files are using altchuncks. But you are trying to replace things as if you are modifying the resulting document's openxml.

When you merge documents as altchuncks you are basically adding them as embedded external files to the original document but not as openxml markup. Which means you cannot treat the additional attached documents as openxml documents.

If you want to achieve what you are trying, you have to merge the documents as explained in my answer here - https://stackoverflow.com/a/18352219/860243 which makes the resulting document a proper openxml document. Which allows you to modify it later as you wish.



来源:https://stackoverflow.com/questions/30285920/search-and-replace-text-in-openxml-added-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!