Issue Excel found unreadable content

Aug 17, 2010 at 5:15 PM
Edited Aug 17, 2010 at 5:20 PM
Excel found unreadable content in 'sample1.xlsx'. Do you want to recover the contents of this workbook. I've download the code of EPPlusWebSample and executed and it runs perfectly and i can download excel file, no message , but when wrote same kind of code in my application and gave reference to (EPPlus.dll 2.7.0.1) and run the application . Now i am getting below message when download excel file . Excel found unreadable content in 'sample1.xlsx'. Do you want to recover the contents of this workbook. below code i am using using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using OfficeOpenXml; using System.IO; using OfficeOpenXml.Style; using System.Drawing; private void Sample1() { ExcelPackage pck = new ExcelPackage(); var ws = pck.Workbook.Worksheets.Add("Sample1"); ws.Cells["A1"].Value = "Sample 1"; ws.Cells["A1"].Style.Font.Bold = true; var shape = ws.Drawings.AddShape("Shape1", eShapeStyle.Rect); shape.SetPosition(50, 200); shape.SetSize(200, 100); shape.Text = "Sample 1 saves to the Response.OutputStream"; pck.SaveAs(Response.OutputStream); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AddHeader("content-disposition", "attachment; filename=Sample1.xlsx"); } What could be the issue, really appreciate your help Thanks In Advance reddy
Editor
Aug 17, 2010 at 5:30 PM

Call response.clear before send stream back to client.

                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                   
                    Response.BinaryWrite(pck.GetAsByteArray());
                    Response.End();

Aug 17, 2010 at 5:33 PM
Thanks a lot, it works Really appreciate your quick response
Oct 13, 2011 at 3:54 AM

Confirmed. This works!!! Thanks.

Feb 1, 2012 at 9:47 PM

The clear/end pair of calls did the trick for me.

It would be great if the web samples worked out of the box. I wasted time looking for the cause of that issue.

Would it be possible to update the web samples so they properly handle the response?

Thanks.

Feb 15, 2012 at 6:43 PM
mrxrsd wrote:

Call response.clear before send stream back to client.

                    Response.Clear();
                    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");
                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                   
                    Response.BinaryWrite(pck.GetAsByteArray());
                    Response.End();

 

Thanks mrxrsd, your code works fine in browser other than IE. I am using IE 8.0.

I am populating pivot table through EPPlus. It works fine with other browser(Chrome, FF) but not working with IE.

Seeking help on this.

Editor
Feb 16, 2012 at 2:33 PM

Humm, strange, all my codes works fine in IE too. Maybe it is another problem, if you find out something, tell us here =)

Did you try any code without pivot table ?  

May 30, 2012 at 2:42 AM

Im having the same problem, is there any alternative to clear/end?, Im currently unable to use Response.End() because of an updatePanel I have, and I can´t take this outside the updatePanel.

Editor
May 30, 2012 at 12:18 PM

I didn´t understand your problem, What´s the relantionship between Response.End() and updatePanel ?

May 30, 2012 at 6:20 PM
Edited May 30, 2012 at 6:40 PM

Thank you for your time.
Im trying to export to excel from ms sql 2005 database, using a dataset with 2 dataTables in C# visual studio 2008 

code snippet:  

 public static MemoryStream DataSetToXlsx(DataSet ds)
        {
           
                MemoryStream Result = new MemoryStream();
                ExcelPackage pack = new ExcelPackage();

                foreach (DataTable tbl in ds.Tables)
                {
                    ExcelWorksheet ws = pack.Workbook.Worksheets.Add(tbl.TableName);
                    ws.Cells["A1"].LoadFromDataTable(tbl, true);
                }

 		pack.SaveAs(Result);
                return Result;
      }

finally...

Response.Clear();
                //ExcelStream is the result from the DataSetToXlsx method
				OnlineExportResultStatic.ExcelStream.WriteTo(Response.OutputStream);
                Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                Response.AddHeader("Content - Disposition", "attachment;filename=dataSet.xlsx");
                //Response.End();                
	HttpContext.Current.ApplicationInstance.CompleteRequest();

I don´t now if the response clear/end is the actual problem to my unreadable content issue but i can´t use Response.End due to some thread abort exception.Excel throws the unreadable content message, I choose "yes" recover information, diplays a message saying that something was repaired, nothing in specific:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?> 
<recoveryLog xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
  <logFileName>error011720_01.xml</logFileName> 
  <summary>Se han detectado errores en el archivo "C:\Documents and Settings\My Documents\dataSet.xlsx"</summary> 
<additionalInfo>
  <info>blabla spanish for: Excel completed some validation and repairing, someparts may have been excluded.</info> 
  </additionalInfo>
  </recoveryLog>

The result is 2 sheets with correct column name and values

I am using this dll: EPPlus 3.0 Binaries

I appreciate your help. 

Editor
May 30, 2012 at 6:31 PM
Edited May 30, 2012 at 7:00 PM

Response.End() will raise a ThreadAbortException, wrap your code with try-catch block.

May 30, 2012 at 6:57 PM

It works, thank you very much.

Jun 26, 2012 at 3:13 AM

I created an account with Codeplex just to say this worked - Thanks!!!

Jun 28, 2012 at 5:21 PM
Edited Jun 28, 2012 at 5:29 PM

I was running into this issue.  After inspecting the generated XLSX file, it appears an error message from my catch block was being appended to the XLSX file.  I solved the issue by ignoring ThreadAbortExceptions.

 

 

try
{
    byte[] reportData = GetReportData();
    Response.Clear();
    Response.AddHeader("content-disposition", "attachment;  filename=" + reportTitle);
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
    Response.BinaryWrite(reportData);
    Response.End();

}
catch (Exception ex)
{
    if (!(ex is System.Threading.ThreadAbortException))
    {
        //Other error handling code here
    }
}

Sep 6, 2012 at 4:26 PM

Clyde, you sorted me out.

I was originally using "Response.End()" but was getting the "Thread was being aborted" error. So followed Microsoft advise and replaced it with "HttpContext.Current.ApplicationInstance.CompleteRequest()". This allowed me to create the Excel file but when opening the file it started throwing the error "Excel found unreadable content in...". The content of the Excel file was just basic table with a few columns and text in them. Nothing fancy. However using "Response.End()" with the ignorring of the ThreadAbortException solved all issues (for the moment ;) )

Once again thanks,

/t

Feb 7, 2013 at 6:02 AM
Works like charm..!! thank you very much..!!
May 31, 2013 at 11:26 AM
In case someone else has the same problem...

For me, the issue continued until I removed the output from within a Using statement, i.e.
Using pck As New ExcelPackage
       'do lots of stuff to create package
       Response.Clear()
        Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
        Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx")
        Response.BinaryWrite(pck.GetAsByteArray())
        Response.End()
End Using
needs to be replaced with
 Dim pck As New ExcelPackage
 'do lots of stuff to create package
 Response.Clear()
 Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
 Response.AddHeader("content-disposition", "attachment;  filename=ExcelDemo.xlsx")
 Response.BinaryWrite(pck.GetAsByteArray())
 Response.End()
Jul 10, 2013 at 9:50 AM
mrxrsd wrote:
Call response.clear before send stream back to client.                     Response.Clear();                    Response.AddHeader("content-disposition", "attachment;  filename=file.xlsx");                    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";                                        Response.BinaryWrite(pck.GetAsByteArray());                    Response.End();
Perfect... Thank you so much...
Sep 13, 2013 at 5:26 AM
I also solved my problem!! Thank you.

First , my code like below , it was not work ( downloaded excel is broken ).
Dim file As MemoryStream = New MemoryStream()
_pkg.SaveAs(file)
file.GetBuffer()