Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在OPENXML中搜索和替换文本(添加的文件)_C#_Ms Word_Openxml_Openxml Sdk - Fatal编程技术网

C# 在OPENXML中搜索和替换文本(添加的文件)

C# 在OPENXML中搜索和替换文本(添加的文件),c#,ms-word,openxml,openxml-sdk,C#,Ms Word,Openxml,Openxml Sdk,我知道上面有很多帖子,但没有任何东西能解决我的问题: 我使用OPENxml创建word文档,并在创建过程中向文档中添加一些现成的文件。我想更改文档准备就绪后添加的文件中的一些文本。这就是我所尝试的: 首先创建文档: fileName = HttpContext.Current.Server.MapPath("~/reports/"+fileName+".docx"); using (var doc = WordprocessingDocument.Create( fileName, Wo

我知道上面有很多帖子,但没有任何东西能解决我的问题: 我使用OPENxml创建word文档,并在创建过程中向文档中添加一些现成的文件。我想更改文档准备就绪后添加的文件中的一些文本。这就是我所尝试的: 首先创建文档:

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);
}
以下是我添加文件的方式:

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后)尝试替换文本的方式

第一次尝试:

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");
                    }
                }
            }
        }
    }
}
使用(WordprocessingDocument文档=
WordprocessingDocument.Open(@“yourpath\testdocument.docx”,true))
{
var body=doc.main documentpart.Document.body;
var-paras=body.Elements();
foreach(第4-4段中的第4段)
{
foreach(变量在para.Elements()中运行)
{
foreach(run.Elements()中的变量文本)
{
if(text.text.Contains(“要替换的文本”))
{
text.text=text.text.Replace(“要替换的文本”、“替换的文本”);
}
}
}
}
}
}
没有一个有效,我尝试了更多。 它适用于我手动添加到文档中的文本,但现在适用于我从就绪文件中添加的文本。
有办法吗?

添加文件的方法是使用
altchunck
s。但是,您试图替换这些内容,就好像您正在修改结果文档的
openxml

当您将文档合并为
altchuncks
时,基本上是将它们作为嵌入的外部文件添加到原始文档中,而不是作为
openxml
标记。这意味着您不能将附加的文档视为
openxml
文档


如果您想要实现您正在尝试的目标,您必须按照我在这里的回答中所解释的那样合并文档-这使得生成的文档成为一个适当的
openxml
文档。这使您可以在以后根据需要对其进行修改。

在您将创建的文档打开并保存到MS Word中之前,这两种方法都不起作用。使用altChunk导入wordML内容时,它会在第一个文档中按原样添加文档(作为外部内容)。在此阶段,创建的文档只包含对该外部内容的引用(并且没有段落,没有运行…),然后当您在MS Word中打开创建的文档并保存它时,MS Word将把添加的文件转换为OpenXml内容。不确定什么是满足您需求的最佳方法。