Question Saving Excel to a Stream and Not a File

BrianBu

New member
Joined
Jan 12, 2010
Messages
3
Programming Experience
10+
This is really an Excel in VB question but I didn't find an office section so I'm posting this to general.

Background: I work with people who have an unhealthy relationship with Excel. We have a nice ASP.NET MVC application going and now they want to save everything down to Excel. Typical.

So I can make an Excel document on the fly, save down to the disk and then return a file to the user (see below). But now I'm going to get a million orphan local copies of these Excel files on the server and have to clean them up. I was wondering if anyone knew how to just save the Excel object straight into a stream and skip it every being a real "file."

Here's what I have so far:
Function Excel() As FilePathResult
Dim app As New Excel.Application
Dim wb As Excel.Workbook = app.Workbooks.Add
Dim ws As Excel.Worksheet = wb.Sheets(1)
ws.Range("A1").Value = "Hello"

Dim sName As String = UserName & "_" & Date.Now.Year & "_" & Date.Now.Month & "_" & Date.Now.Day & "_" & Date.Now.Hour & "_" & Date.Now.Minute & "_" & Date.Now.Second & ".xls"

wb.SaveAs(System.AppDomain.CurrentDomain.BaseDirectory & sName)
wb.Close()
app.Quit()

Return File(System.AppDomain.CurrentDomain.BaseDirectory & sName, "application/ms-excel", sName)
End Function

What I want to do is more like this:
Function Excel As FileStreamResult
'Make the Excel workbook
dim stream as IO.SomeStream = 'This is the missing magic step
return File(stream, "application/ms-excel", sName)
End Function

Any help would be appreciated

Thanks
Brian
 
Back
Top