C# 将修改后的WordprocessingDocument保存到新文件

C# 将修改后的WordprocessingDocument保存到新文件,c#,openxml,openxml-sdk,office-2007,C#,Openxml,Openxml Sdk,Office 2007,我试图打开Word文档,更改一些文本,然后将更改保存到新文档。我可以使用下面的代码完成第一部分,但我不知道如何将更改保存到新文档(指定路径和文件名) 我是一个完全的初学者,所以请原谅这个基本问题 如果使用内存流可以将更改保存到新文件,如下所示: byte[] byteArray = File.ReadAllBytes("c:\\data\\hello.docx"); using (MemoryStream stream = new MemoryStream()) { stream.Wri

我试图打开Word文档,更改一些文本,然后将更改保存到新文档。我可以使用下面的代码完成第一部分,但我不知道如何将更改保存到新文档(指定路径和文件名)


我是一个完全的初学者,所以请原谅这个基本问题

如果使用
内存流
可以将更改保存到新文件,如下所示:

byte[] byteArray = File.ReadAllBytes("c:\\data\\hello.docx");
using (MemoryStream stream = new MemoryStream())
{
    stream.Write(byteArray, 0, (int)byteArray.Length);
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(stream, true))
    {
       // Do work here
    }
    // Save the file with the new name
    File.WriteAllBytes("C:\\data\\newFileName.docx", stream.ToArray()); 
}
对我来说,工作很好:

// To search and replace content in a document part.
public static void SearchAndReplace(string document)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
    {
        string docText = null;
        using (StreamReader sr = new StreamReader(wordDoc.MainDocumentPart.GetStream()))
        {
            docText = sr.ReadToEnd();
        }

        Regex regexText = new Regex("Hello world!");
        docText = regexText.Replace(docText, "Hi Everyone!");

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

只需将源文件复制到目标文件并从那里进行更改

File.copy(source,destination);
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(destination, true))
    {
       \\Make changes to the document and save it.
       wordDoc.MainDocumentPart.Document.Save();
       wordDoc.Close();
    }
希望这能奏效。

在OpenXMLSDK2.5中:

    File.Copy(originalFilePath, modifiedFilePath);

    using (var wordprocessingDocument = WordprocessingDocument.Open(modifiedFilePath, isEditable: true))
    {
        // Do changes here...
    }
默认情况下,
wordprocessingDocument.AutoSave
为true,因此Close and Dispose将保存更改。 不需要显式地使用
wordprocessingDocument.Close,因为使用块将调用它


这种方法不需要像accepted answer中那样将整个文件内容加载到内存中。对于小文件来说这不是一个问题,但在我的例子中,我必须同时处理更多带有嵌入式xlsx和pdf内容的docx文件,这样内存使用率就会非常高。

这种方法允许您缓冲“模板”文件,而无需将整个文件批处理为
字节[]
,也许可以让它的资源密集度降低

var templatePath = @"c:\data\hello.docx";
var documentPath = @"c:\data\newFilename.docx";

using (var template = File.OpenRead(templatePath))
using (var documentStream = File.Open(documentPath, FileMode.OpenOrCreate))
{
    template.CopyTo(documentStream);

    using (var document = WordprocessingDocument.Open(documentStream, true))
    {
        //do your work here

        document.MainDocumentPart.Document.Save();
    }
}

1.看起来您想编写
wordDoc.Save()
。2.没有
Save()
方法。所以这对我来说似乎不是一个有效的解决方案。有什么我遗漏的吗?请注意,此代码
main documentpart.Document.Save()
不会保存整个软件包/word文档。还有其他部分也需要保存(我的问题是脚注没有保存,它们不在
MainDocumentPart
下)。我最终使用了接受的答案,而不是试图找出需要调用save的位。对于那些您单独保存的位。在Open XML SDK 2.5中,不必显式调用save,如果WordprocessingDocument.AutoSave为true,则Close and Dispose将保存。Close也不必显式调用,因为using块将调用Dispose,而Dispose将调用Close。这会将它保存到同一个文档中,因此不会回答问题。对于那些通过Google访问到这一点的用户,我们基本上只是在遵循这一点:除了我们试图将其保存到新文档中,不是原版的。很好地适应了当前的情况。也可以使用(FileStream FileStream=newfilestream(“C:\\data\\newFileName.docx”,System.IO.FileMode.CreateNew)){stream.WriteTo(FileStream);}
而不是
File.writealBytes(…)
如果希望只获取字节,可以执行
byteArray=stream.ToArray()在“WordDOC”<代码>使用块后,在中间关闭。如果这个解决方案对您不起作用,只需在“//do-Debug”结束时写下以下行:WordDoc .Couter();此命令实际上是将所有内容写入流的命令。
var templatePath = @"c:\data\hello.docx";
var documentPath = @"c:\data\newFilename.docx";

using (var template = File.OpenRead(templatePath))
using (var documentStream = File.Open(documentPath, FileMode.OpenOrCreate))
{
    template.CopyTo(documentStream);

    using (var document = WordprocessingDocument.Open(documentStream, true))
    {
        //do your work here

        document.MainDocumentPart.Document.Save();
    }
}