Cum Grano Salis

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)

Published Friday, August 03, 2007 1:22 PM by Shahar

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

 

manasi said:

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

September 10, 2007 10:59 AM
 

Shahar said:

What controls are you refering to?

How does the mechanism for downloading help?

September 10, 2007 1:41 PM
 

manasi said:

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

September 10, 2007 2:34 PM
 

Shahar said:

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

September 10, 2007 2:36 PM
 

fyz said:

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

April 23, 2008 4:15 AM
 

Jessica said:

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

June 16, 2008 12:41 PM
 

Gayatri said:

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

July 4, 2008 5:59 AM

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required
Submit

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Microsoft
Page view tracker