Excel found unreadable content in Workbook. Do you want to recover the contents of this workbook?


When trying to stream back the resulting byte-array of the GetWorkbook() from ASP.NET, you may run into this issue. I just ran into it yesterday and today I saw a post about it in the Excel Services forums, so I thought I’d explain what’s probably going on.

(I can’t answer on the forums because the freaking thing is again in that quasi-logged out state where I can’t do anything or answer questions. SO annoying.)

If you open the file you streamed in a binary editor (or notepad for that matter), you will probably see at the bottom some HTML text. That text is there because ASP.NET, once it finished outputting the binary contents of the workbook via the Response.OutputStream.Write(…) method, continued to write HTML into the file. That causes “garbage” to be written to the end of the file which in turn causes Excel to show the error at the top of this post.

To solve the issue, call Request.EndResponse() when you finish writing the binary data into the Response.OutputStream object. That will cause ASP.NET to stop dead in its tracks and return only the data you have written.

Now, that “Stop dead” in its track thing is the important bit. What actually happens is that Request.EndResponse actually injects a ThreadAbortException into the thread, causing the call to exit completely. The issue I was having is related to that.

As part of my rendering code, I had a path that would write to the HTML any error that occurred. This helps with debugging issues with the page that is hosted by SharePoint, which is a fun thing to do. So my code looked something like this:

try
{
   // Do stuff..
   Page.Response.OutputStream.Write(…);
   Page.Response.EndResponse();
}
catch (Exception ex)
{
   Page.Response.OutputStream.Write(“An error occurred: ” + ex.ToString());
}

The net result of this is that each of my produced Excel files had the .ToString() of a ThreadAbortException tacked at the end.

Catching ThreadAbort specifically and not outputting anything there did the trick and produced proper files.

(Thanks go to Boaz who helped me figure out what was wrong with the files)


Comments (14)

  1. manasi says:

    It did solve the corrupted file problem. But response.end freezes the page after file download and all the controles are disabled. Thats not good

    By the way how did u get to Page.Response.EndResponse();

    I dont see EndResponse() method for my response object. Did u use some different library? I am inheriting from system.web.ui.page which has response.end().

    Thanks

  2. Shahar Prish says:

    What controls are you refering to?

    How does the mechanism for downloading help?

  3. manasi says:

    I will post this on the forum. Could you respond over there. Thank you for the quick response

  4. Shahar Prish says:

    I go to the forum once every other day or so. I will answer you there (thanks for doing this!)

  5. fyz says:

    Hi,

    I am not good at Excel programming. But when I got unreadable xls file, I would rather try a utility called Advanced Excel Repair to repair your Excel xls file. It works rather well for me. Its web address is http://www.datanumen.com/aer/  Maybe this will help.

    Alan

  6. Jessica says:

    Hi,

    I’ve tried Data Numen’s tool but I had better results with ExcelFix by Cimaware. I found that the price – quality relationship in ExcelFix is better than Data Numen’s.

    You can check it out here: http://www.cimaware.com/main/products/excelfix.php

    Good luck!

    Jess

  7. Gayatri says:

    Perfect – this was bugging me since two weeks – I just used Response.End() and caught the exception. Thanks!

  8. Vikas says:

    Thanks a tone man u saved my lots of time

  9. Sravan says:

    Thanks a lot. It solved my problem 🙂

  10. Swapnil says:

    Thank you very much for posting this. It helped me to resolve my issue.

    Thanks,

    Swapnil

  11. Gilberto Couto says:

    Thanks a Lot! Response.End(0 solved my problem!

  12. Pradeep says:

    Response.Flush();

    Response.SuppressContent = true;

    Worked for me.

  13. Brad says:

    Pradeep….a day in…and your fix worked for me…..thanks!

  14. Sopan Maiti says:

    Thanks Pradip. Your fix also worked for me.