Save modified WordprocessingDocument to new file

前端 未结 5 669
礼貌的吻别
礼貌的吻别 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: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()); 
    }
    

提交回复
热议问题