This project has moved and is read-only. For the latest updates, please go here.

Microsoft Excel has stopped working

Mar 14, 2013 at 8:35 PM
I am in the process of creating a web application to create and display an Excel spreadsheet when I click a button. I decided to start simple and canibalize the sample web app.

When I click on the button Internet Explorer displays "Do you want to open or save ...", so I click OPEN. Then an EXCEL session starts followed by a dialog saying "Microsoft Excel has stopped working - Windows is checking for a solution to the problem...". Then (without touching the cancel buton) a second dialog replaces the first saying "Microsoft Excel is trying to recover your information - This might take several minutes." And then (without touching the Cancel button) a third dialog pops up saying "Microsoft Excel has stopped working - A problem caused the program to stop working correctly. Windows will close the program and notify you if a solution is available."

But if I click SAVE or SAVE AS instead of OPEN, the file saves fine and when I double click it, it opens without problem.

Here's my code:
        Dim excel As New ExcelPackage()
        Dim wkst As ExcelWorksheet = excel.Workbook.Worksheets.Add("Sample2")

        wkst.Cells("A1").Value = "Sample 2"
        wkst.Cells("A1").Style.Font.Bold = True

        Response.ClearHeaders()
        Response.BinaryWrite(excel.GetAsByteArray())
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;  filename=Sample2.xlsx")
        Response.End()
Can anyone offer any insight into what I'm doing wrong?
Mar 14, 2013 at 9:29 PM
Edited Mar 15, 2013 at 12:06 PM
Response.End() is causing a ThreadAbortException. I tried adding a TRY/CATCH around the code, but that didn't stop the error.

When I remove the Response.End(), the EXCEL message changes to "Excel found unreadable content in 'Sample2.xlsx'. Do you want to recover the contents of the workbook? If you trust the source of this workbook, click Yes." Once I click YES the file opens just fine.

Could the problem have to do with the UpdatePanel(s) I'm using?
Mar 14, 2013 at 9:30 PM
Is there a way to "roll back" to the original response if some other exception is encountered?