在OPENXML中搜索和替换文本(添加的文件) [英] Search And Replace Text in OPENXML (Added file)

查看:127
本文介绍了在OPENXML中搜索和替换文本(添加的文件)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道上面有很多帖子,但是对于我的问题没有任何作用: 我正在使用OPENxml创建Word文档,并且在创建过程中将一些准备好的文件添加到文档中.我想在文档准备好后更改要添加的文件中的一些文本.这就是我尝试过的: 首先创建文档:

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

这是我在创建文件后(尝试使用WordprocessingDocument后)替换文本的方式

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

第一次尝试:

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

第二次尝试:

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?

推荐答案

添加文件的方式是使用altchunck.但是,您试图替换事物,就好像您正在修改生成的文档的openxml一样.

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.

当您将文档合并为altchuncks时,基本上是将它们作为嵌入式外部文件添加到原始文档中,而不是作为openxml标记添加.这意味着您不能将其他附加文档视为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.

如果您想实现自己的目标,就必须按照我的回答在此处合并文档- https://stackoverflow.com/a/18352219/860243 ,使生成的文档成为正确的openxml文档.这样一来,您便可以根据需要稍后对其进行修改.

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.

这篇关于在OPENXML中搜索和替换文本(添加的文件)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆