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.
Madhu Sudhanan Ramanujam - 22 Jun 2005 12:09 GMT
Hi,
Can you send your full code please.
Madhu Sudhanan Ramanujam
> We create an excel object with Excel/Office Interop Assembly and we want
> to offer this excel object as download without creating physically the
[quoted text clipped - 75 lines]
>
> Thanks.
Madhu Sudhanan Ramanujam - 24 Jun 2005 10:03 GMT
Hi George,
I got your code and I tried working with it. After a good amount of trying I
just feel that we'll have to write the Excel Object to a file. The reason is
that the PIA doesn't provide any option by which we can save the Excel
object's content into a stream or some thing. So your plan of streaming it
directly to the browser can't be worked out is what I feel. However try to
see if you can find some thing about this. If I come across some thing then
I'll let you know and I request you to tell me if you found some thing.
Madhu Sudhanan Ramanujam
> We create an excel object with Excel/Office Interop Assembly and we want
> to offer this excel object as download without creating physically the
[quoted text clipped - 75 lines]
>
> Thanks.
news.central.cox.net - 02 Sep 2005 18:40 GMT
I've been doing the same thing on a project of mine. I've not found a way to
offer it for download without writing it to a physical file.
I also put in some code in the Session_End part of Global.asax to delete any
files created during the session.
' Attempt to delete template spreadsheet file (If it exists)
Try
File.Delete(Application("BaseDirectory") & "<filename goes here, removed
for example" & ".xls")
Catch ex As Exception
' Just ignore if deletion fails (May not be a file to delete)
End Try
I also put in another 'just in case' in the Application_Start to knock out
everything in the temp directory on startup just in case the Session_End was
unable to run (Power outage, etc.):
' Get rid of any abandoned template files in the Download directory.
For Each Filename In Directory.GetFiles(Application("BaseDirectory") &
"Download", "*.x*")
File.Delete(Filename)
Next
> We create an excel object with Excel/Office Interop Assembly and we want
> to offer this excel object as download without creating physically the
[quoted text clipped - 75 lines]
>
> Thanks.
Uday Takbhate [MSFT] - 19 Sep 2005 07:14 GMT
Hello George,
Your solution seems to impliment server side automation of the office,
which is not recommended and supported by Microsoft. Please refer to the
following article for more information,
INFO: Considerations for Server-Side Automation of Office
http://support.microsoft.com/?id=257757
I will suggest you to avoid the use of server side solution if your
business need does not demand for the same, as there are certain known
problems with the server side automation, such as:
- Security issues
- Reentrancy and scalability
- Stability
- Upgrade issues
- Licensing implications
If your business logic permits then you may opt for a solution which does
not utilize the server side automation.
Refer following article which talks about such a solution
How To Export Data in a DataGrid on an ASP . NET WebForm to Microsoft Excel
http://support.microsoft.com/default.aspx?scid=KB;en-us;317719
How To Use ASP to Build Spreadsheet XML for Client-Side Display
http://support.microsoft.com/kb/q288130/
ExcelADO demonstrates how to use ADO to read and write data in Excel
workbooks
http://support.microsoft.com/default.aspx?scid=KB;EN-US;278973
How To Query and Update Excel Data Using ADO From ASP
http://support.microsoft.com/kb/195951/
How To Use ADO with Excel Data from Visual Basic or VBA
http://support.microsoft.com/kb/257819/EN-US/
Information about how to extract Office file formats and schemas
http://support.microsoft.com/kb/840817/EN-US/
How To Display ASP Results Using Excel in IE with MIME Types
http://support.microsoft.com/kb/q199841/
How To Format Cell Data When You Are Creating an Excel File With an Active
Server Pages Page
http://support.microsoft.com/kb/q260239/
How To Create an Office Document in an ASP Application
http://support.microsoft.com/default.aspx?scid=kb;en-us;301044
I hope this information helps you!
Regards,
Uday Takbhate [MSFT]
Microsoft Developer Support
--------------------
>From: "news.central.cox.net" <nobody@here.com>
>Newsgroups: microsoft.public.dotnet.framework.interop
[quoted text clipped - 14 lines]
>Organization: Cox Communications
>Path:
TK2MSFTNGXA01.phx.gbl!TK2MSFTNGP08.phx.gbl!newsfeed00.sul.t-online.de!t-onli
ne.de!newshub.sdsu.edu!peer01.west.cox.net!cox.net!p01!okepread02.POSTED!53a
b2750!not-for-mail
>Xref: TK2MSFTNGXA01.phx.gbl microsoft.public.dotnet.framework.interop:9417
>X-Tomcat-NG: microsoft.public.dotnet.framework.interop
[quoted text clipped - 102 lines]
>>
>> Thanks.