none
Server Side Excel documents RRS feed

  • Question

  • I'm trying to do something which seems rather straight forward, but I keep getting a Thread was being aborted exception when trying to do this:

    protected void GenerateFauxXLSSheet()  
    {  
        String _template = Request.PhysicalApplicationPath + @"reports\Template_Report.xlsx";  
        String _generated = Request.PhysicalApplicationPath + @"reports\" + machineName + "_report.xlsx";  
        File.Copy(_template, _generated, true);  
     
        using (SpreadsheetDocument xlDoc = SpreadsheetDocument.Open(_generated, true))  
        {  
            // Do Work       
     
            // Close the workbook  
            xlDoc.Close();  
        }  
     
        // Now hand back the new sheet  
        try  
        {  
            Response.Redirect(@"reports\" + machineName + "_report.xlsx");  
        }  
        catch (Exception ex)  
        {  
            EventLog.WriteEntry("myTool", "Error: " + ex.Message, EventLogEntryType.Error, ErrorCodes.MyTool.E_FILE_SYSTEM_FAILURE);  
            Response.Redirect("Error.aspx");  
        }  
     I had some other work in there, but simplified it down to copying the template, opening the destination file, closing it, and giving it back to the user.  The odd thing is that if I don't redirect to the user, and I open the file in the file system, all of my modifications are there and the file looks great.  If I then load a seperate page that just does the redirect, it works fine.  Something is not kosher about how I'm closing the document before the redirect.

    Help...
    Wednesday, December 17, 2008 11:29 PM

Answers

  • As I recall, Response.Redirect almost always throws a ThreadAbortException (there's a reason, but I don't recall it off-hand, Google for details).  Since it's normal, you can place the Response.Redirect outside the try/catch or remove it if that's all it contains.  If you need it within a try/catch, just add a catch(ThreadAbortException) handler that eats it and does nothing.

    Eric
    • Proposed as answer by EWoodruff Monday, December 22, 2008 9:21 PM
    • Marked as answer by John S Blackman Monday, December 22, 2008 9:33 PM
    Monday, December 22, 2008 9:21 PM

All replies

  • As I recall, Response.Redirect almost always throws a ThreadAbortException (there's a reason, but I don't recall it off-hand, Google for details).  Since it's normal, you can place the Response.Redirect outside the try/catch or remove it if that's all it contains.  If you need it within a try/catch, just add a catch(ThreadAbortException) handler that eats it and does nothing.

    Eric
    • Proposed as answer by EWoodruff Monday, December 22, 2008 9:21 PM
    • Marked as answer by John S Blackman Monday, December 22, 2008 9:33 PM
    Monday, December 22, 2008 9:21 PM
  • Worked perfectly.  Thank you for the insight to the Response.Redirect.
    Monday, December 22, 2008 9:34 PM