Problem opening Excel file


I have a problem with opening a created Excel file. We have a web application that generates reports, in this case a simple report detailing amounts of revenue generated by a company.
Every time the report runs and downloads to the users browser the user gets the following error:
Excel found unreadable content in *****.xlsx. Do you want to recover the workbook?
I attach the file so you can analyze the problem

file attachments


vlad wrote Sep 6, 2007 at 9:34 AM

I have the same problem. Web application, VB.NET, attached you have the generated file.The issue is that "xl\worksheets\sheet1.xml" starts like this: <?xml version="1.0" encoding="utf-8"?>As you can see there are some strange characters at the start of the file. However, opening the file in notepad, etc ... doesn't show the character. I've sone a compare by content with total commander to see the error. The additional HEX codes are: EF BB BF and then the <?xml?> tag starts.

However, after removing those charasters, the file is still incorrect.

wrote Sep 6, 2007 at 9:34 AM

wrote Sep 6, 2007 at 9:36 AM

BlackBar wrote Nov 2, 2007 at 9:37 AM

I have the same problem, too! Maybe someone have a solution?

wrote Sep 16, 2008 at 12:30 PM

wrote Feb 10, 2009 at 2:15 PM

PBalanagendra wrote Jan 11, 2010 at 3:42 AM

I am still facing this problem. Please let me know if there is any resolution to this issue.

PBalanagendra wrote Jan 11, 2010 at 5:51 AM

I got to find the root cause for this error, atleast for my case.

Cause: After flushing the contents of the Excel sheet to the Response stream in a web application, script to close the window was also sent as part of the response.stream. When viewed the generated excel in the notepad, the javascript script was also part of the Excel content and when opened Excel use to throw the message - unreadable content.

Resolution: I removed the part which puts the script to response stream and the generate Excel opened without any error.

It might not be the same cause and resolution for this kind of error. However, it has to do something with the content in the Excel file which cannot be readable by Excel.

wrote Jan 11, 2010 at 5:53 AM

sowji250 wrote Feb 3, 2010 at 5:45 PM

Hi PBalanagendra,
I have a requirement in one of our web applications, when the user clicks on excel export button, it should generate an excel file by loading the data from datatable and should be able to open it on th fly (with out saving it on server) for the user to view it.

I have figured out how to load data from datatable, but the unknown part is to generate the excel file on th fly and open it fofr the user to view.

Can you please let me know if you have any idea how to do this?


PBalanagendra wrote Feb 4, 2010 at 3:24 AM

Once the Excel is generated on the server, you can flush the Excel contents to the output stream and then delete the Excel. Below is the code to perform this operation

// Write the Excel content to the output stream and delete the file
string fileName = ""; // Specify the Excel File path

Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment;filename=CustomBRSReport" + (hFormat.Value == "2003" ? ".xls" : ".xlsx"));

if (File.Exists(fileName))


sowji250 wrote Feb 4, 2010 at 3:14 PM

Hi PBalanagendra,

Thankyou for posting the solution. Actually I tried this code yesterday and now I had another issue.
When trying to get the stream, I get System.OutOfMemoryException when dealing large amount of data (for ex 90,000 rows with 20 columns). Do you know if we can fix this?

Thanks in advance.

wrote Feb 22, 2013 at 12:31 AM

dschonhals wrote Oct 2, 2013 at 5:45 PM

Issue opening file was due to "Responde.End()" missing in my code. Also don't forget to add Response.Clear() or Response.ClearContent() in the begining.