ASP.NET + Excel Download

G

George Homorozeanu

We create an excel object with Excel/Office Interop Assembly and we want to
offer this excel object as download without creating physically the file.

The download window appears with "Save As.", "Open", etc



Problem:

The file we get as download is not the excel object we created. Instead of
the correct values/data we just get the namespace from the excel object in
the downloaded file.



Source code:

Private Sub SetExcel()

Dim ea As New Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet

wb = ea.Workbooks.Add(1)

ws = wb.ActiveSheet

ws.Name = "Details"

ws.Cells(1, 1) = "test"
ws.Cells(4, 4) = "hallo"
ws.Cells(5, 6) = 4324
ws.Cells(3, 5) = "tja"

Call DownloadFromText(wb)

ea.Quit()

End Sub

Public Sub DownloadFromText( _
ByVal obj As Object)

Dim strHdr As String = ""

strHdr = "attachment;filename=ExcelFile.xls"

With HttpContext.Current.Response

.Clear()
.ContentType = "application/vnd.ms-excel"
.ContentEncoding = System.Text.Encoding.Default
.AppendHeader("Content-Disposition", strHdr)
.Write(obj)
.Flush()
.Clear()
.Close()

End With

End Sub

Output:

An excel file with one single line containing the text:

Microsoft.Office.Interop.Excel.WorkbookClass



We assume:

ContentType or ContentEncoding of HttpContext.Current.Response are not set
correctly



-OR-



The Write(obj) method is not the correct one to use in this case.



Everything works fine when writing first the excel object into a physical
file and offer the download with Response.TransmitFile. But creating the
file is not what we want.



Any suggestions could help us a lot.

Thanks.
 
Joined
Mar 17, 2010
Messages
2
Reaction score
0
Hi George:

When I try your code, the Excel file saves, but when I open it I see the data in a separate worksheet, and there is a new blank worksheet as well.

I have been tweaking it, trying to get it to work, but have had no luck..

Did you ever resolve this issue?

Thanks for your help,
-Krishna

George Homorozeanu said:
We create an excel object with Excel/Office Interop Assembly and we want to
offer this excel object as download without creating physically the file.

The download window appears with "Save As.", "Open", etc



Problem:

The file we get as download is not the excel object we created. Instead of
the correct values/data we just get the namespace from the excel object in
the downloaded file.



Source code:

Private Sub SetExcel()

Dim ea As New Microsoft.Office.Interop.Excel.Application
Dim wb As Microsoft.Office.Interop.Excel.Workbook
Dim ws As Microsoft.Office.Interop.Excel.Worksheet

wb = ea.Workbooks.Add(1)

ws = wb.ActiveSheet

ws.Name = "Details"

ws.Cells(1, 1) = "test"
ws.Cells(4, 4) = "hallo"
ws.Cells(5, 6) = 4324
ws.Cells(3, 5) = "tja"

Call DownloadFromText(wb)

ea.Quit()

End Sub

Public Sub DownloadFromText( _
ByVal obj As Object)

Dim strHdr As String = ""

strHdr = "attachment;filename=ExcelFile.xls"

With HttpContext.Current.Response

.Clear()
.ContentType = "application/vnd.ms-excel"
.ContentEncoding = System.Text.Encoding.Default
.AppendHeader("Content-Disposition", strHdr)
.Write(obj)
.Flush()
.Clear()
.Close()

End With

End Sub

Output:

An excel file with one single line containing the text:

Microsoft.Office.Interop.Excel.WorkbookClass



We assume:

ContentType or ContentEncoding of HttpContext.Current.Response are not set
correctly



-OR-



The Write(obj) method is not the correct one to use in this case.



Everything works fine when writing first the excel object into a physical
file and offer the download with Response.TransmitFile. But creating the
file is not what we want.



Any suggestions could help us a lot.

Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top