How to write an Excel workbook to a MemoryStream in .NET?

calendar_today Asked Oct 1, 2008
thumb_up 6 upvotes
history Updated April 16, 2026

Question posted 2008 · +12 upvotes

How do I write an Excel workbook to a MemoryStream without first saving it to the file system?

All options within the Microsoft.Office.Interop.Excel.WorkBook save options take a filename.

Accepted answer +6 upvotes

I have done extensive work with the PIA and with storing Excel files in a document repository and streaming it out to the browser, and I have not been able to find a solution to using the PIA without first writing the contents to the file system first.

I think that you are going to have to swallow the bullet and deal with the file system as an intermediary. The good news is that you can just give the file a unique name like a guid or use some other tempfilename method (not sure if one exists in .net) and just delete the contents when you are done.

Excel VBA objects referenced (4)

  • Interop.Excel — Using events with Excel objects
  • Interop.Excel — Using Excel worksheet functions in Visual Basic
  • Microsoft.Office — Controlling One Microsoft Office Application from Another
  • Microsoft.Office — List the Name and Office Location of Each Manager Belonging to an Exchange Distribution List

Top excel Q&A (6)

+6 upvotes ranks this answer #141 out of 167 excel solutions on this site .