1

By using OpenXML to manipulating a Word document (as a template), the server application saves the new content as a temporary file and then sends it to user to download.

The question is how to make these content ready to download without saving it on the server as a temporary file? Is it possible to save OpenXML result as a byte[] or Stream instead of saving it as a file?

Babak
  • 3,716
  • 6
  • 39
  • 56

3 Answers3

3

Using this page: OpenXML file download without temporary file

I changed my code to this one:

byte[] result = null;
byte[] templateBytes = System.IO.File.ReadAllBytes(wordTemplate);
using (MemoryStream templateStream = new MemoryStream())
{
    templateStream.Write(templateBytes, 0, (int)templateBytes.Length);
    using (WordprocessingDocument doc = WordprocessingDocument.Open(templateStream, true))
    {
        MainDocumentPart mainPart = doc.MainDocumentPart;           
        ...         
        mainPart.Document.Save();
        templateStream.Position = 0;
        using (MemoryStream memoryStream = new MemoryStream())
        {
            templateStream.CopyTo(memoryStream);
            result = memoryStream.ToArray();
        }
    }
}
BurnsBA
  • 4,347
  • 27
  • 39
Babak
  • 3,716
  • 6
  • 39
  • 56
2

You can create the WordprocessingDocument and then use the Save() method to save it to a Stream.

http://msdn.microsoft.com/en-us/library/cc882497

jn1kk
  • 5,012
  • 2
  • 45
  • 72
1
  var memoryStream = new MemoryStream();
  document.Clone(memoryStream);
Fedor
  • 17,146
  • 13
  • 40
  • 131
Hamid
  • 42
  • 1
  • 3