Save modified WordprocessingDocument to new file

前端 未结 5 645
礼貌的吻别
礼貌的吻别 2020-11-30 03:48

I\'m attempting to open a Word document, change some text and then save the changes to a new document. I can get the first bit done using the code below but I can\'t figure

相关标签:
5条回答
  • 2020-11-30 04:10

    This approach allows you to buffer the "template" file without batching the whole thing into a byte[], perhaps allowing it to be less resource intensive.

    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();
        }
    }
    
    0 讨论(0)
  • 2020-11-30 04:16

    For me this worked fine:

    // 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);
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-30 04:18

    If you use a MemoryStream you can save the changes to a new file like this:

    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()); 
    }
    
    0 讨论(0)
  • 2020-11-30 04:25

    Simply copy the source file to the destination and make changes from there.

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

    Hope this works.

    0 讨论(0)
  • 2020-11-30 04:31

    In Open XML SDK 2.5:

        File.Copy(originalFilePath, modifiedFilePath);
    
        using (var wordprocessingDocument = WordprocessingDocument.Open(modifiedFilePath, isEditable: true))
        {
            // Do changes here...
        }
    

    wordprocessingDocument.AutoSave is true by default so Close and Dispose will save changes. wordprocessingDocument.Close is not needed explicitly because the using block will call it.

    This approach doesn't require entire file content to be loaded into memory like in accepted answer. It isn't a problem for small files, but in my case I have to process more docx files with embedded xlsx and pdf content at the same time so the memory usage would be quite high.

    0 讨论(0)
提交回复
热议问题