5

I am using OpenXML SDK.

The OpenXML SDK creates a method called CreatePackage as such:

public void CreatePackage(string filePath)
{
    using (SpreadsheetDocument package = SpreadsheetDocument.Create(filePath,               SpreadsheetDocumentType.Workbook))
    {
        CreateParts(package);
    }
}

I call it from my program as follows which will create the Excel file to a given path:

gc.CreatePackage(excelFilePath);
Process.Start(_excelFilePath);

I am not sure how to tweak the code such that it gives back a Stream which shows the Excel file vs having it create the file on disk.

Sir Crispalot
  • 4,792
  • 1
  • 39
  • 64
Nate Pet
  • 44,246
  • 124
  • 269
  • 414

1 Answers1

2

According to the documentation for SpreadsheetDocument.Create there are multiple overloads, one of which takes a Stream.

so change your code to:

public void CreatePackage(Stream stream)
{
    using (SpreadsheetDocument package = SpreadsheetDocument.Create(stream,               SpreadsheetDocumentType.Workbook))
    {
        CreateParts(package);
    }
}

And then call it with any valid Stream, for example:

using(var memoryStream = new MemoryStream())
{
   CreatePackage(memoryStream);
   // do something with memoryStream
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193
  • Thanks Jamiec, how would I open up a file (that is not stored on disk) from this memomorySteam? I tried: //byte[] bytes = new byte[memoryStream.Length]; //memoryStream.Read(bytes, 0, (int)memoryStream.Length); //file.Write(bytes, 0, bytes.Length); //file.Close(); //memoryStream.Close(); but that did not do it – Nate Pet Dec 19 '12 at 17:24