Excel found unreadable content in the file

Hello,

I have created a excel file in the 2007 xlsx format. Below is the code I u

Dim workSheet As Cells.Worksheet = workbook.Worksheets(0)

workSheet.Name = "Test"

workSheet.Cells.ImportDataTable(dtInput, True, "A1")

'dtInput is the dataset that contains data.

workSheet.AutoFitColumns()

Dim stream As New System.IO.MemoryStream

workbook.Save(stream, Cells.SaveFormat.Excel97To2003)

HttpContext.Current.Response.Clear()

HttpContext.Current.Response.AddHeader("cache-control", "private")

HttpContext.Current.Response.AddHeader("Content-disposition", "attachment; filename=Test.xlsx;")

HttpContext.Current.Response.AddHeader("Content-type", "application/vnd.ms-excel")

stream.WriteTo(HttpContext.Current.Response.OutputStream)

Response.Flush()

When I try and open the workbook. Excel displays the following message box:

Excel found unreadable content in 'Test.xlsx'. Do you want to recover the contents of
this workbook? If you trust the source of this workbook, click Yes.

I tried saving the same file in xls format and tried opening the file , then there is no error message getting displayed.

It is happening only when I save it as 2007 xlsx format.

Could you please help me in this..I am using Aspose.Cells version 5.1.3.0

Thanks

Hi,


I can see one obvious error in your code. Please change your line of code:
workbook.Save(stream, Cells.SaveFormat.Excel97To2003)
to:
workbook.Save(stream, Cells.SaveFormat.Xlsx)

Thank you.

Hi Amjad,

Thanks for you reply. Yes, that was error in the code that was posted.But even after the change there is the same error message that is coming up.

Could you please look into this.

Thanks

Hi,

The following works fine and should work fine there.
'......... Your code goes here
Dim stream As New System.IO.MemoryStream
workbook.Save(stream, SaveFormat.Xlsx)
Response.Clear()
Response.ContentType = "application/vnd.openxmlformats"
Response.AddHeader("Content-Disposition", "attachment; filename=" + "Test.xlsx")
Dim buffer As Byte() = stream.ToArray()
Response.BinaryWrite(buffer)
Response.End()


By the way, you need to have Excel 2007/2010 installed on your PC to view the file on the fly.

Moreover, you may also try Workbook.Save() method instead of your manually Response object to streaming the Excel file to the client. The following methods works the same way as your manual code.

Sample code:

"

'Save in xlsx format and send the file to user so that he may open the file in

'some application or save it to some location

workbook.Save(Me.Response, "Test.xlsx", ContentDisposition.Attachment, New OoxmlSaveOptions())

Response.End()"

Add the above line of code just after "workSheet.AutoFitColumns()" and delete all your existing code after that. The above code works fine with new versions. See the document for reference:
http://www.aspose.com/documentation/.net-components/aspose.cells-for-.net/saving-files.html

Thank you.