Link to home
Start Free TrialLog in
Avatar of Ryla
Ryla

asked on

Using Interop.Excel to generate excel file and export to excel

I am using the interop.excel to generate an excel file that I will be exporting to excel. It works fine if I save it to my C drive. But I want to be able to export to excel and give the user a file download prompt with the opens save, open or cancel. If I use
Response.AddHeader("content-disposition","attachment;filename=Hello.xls")
Response.ContentType="application/vnd.ms-excel"
Response.Write(MyWorkBook)
my opened window hangs. Any suggestions?
Avatar of kishoreb123
kishoreb123

pl try Response.BinaryWrite()
Avatar of Neil Fleming
try using a stringwriter:


Dim sw As New StringWriter()
       httpContext.Current.Response.ClearContent()
        HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" & filename)
        HttpContext.Current.Response.ContentType = "application/vnd.xls"
        HttpContext.Current.Response.Charset = ""

HttpContext.Current.Response.Write(sw.ToString())
HttpContext.Current.Response.End()

sw.Dispose()

Open in new window

Avatar of Ryla

ASKER

NeilFleming1, Do you think this will solve the Excel 2007 issue of the warning "The file you are trying to open, 'filename.xls', is in a different format than specified by the file extension. Verify that the file is not corrupted and is from a trusted source before opening the file. Do you want to open the file now?"
ASKER CERTIFIED SOLUTION
Avatar of Neil Fleming
Neil Fleming
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS Microsoft's own advice is to ignore the warning -- the file opens fine in any case.