Open excel file from memorystream

Posted on 2011-05-05
Last Modified: 2012-05-11
I am storing Excel files in an Oracle database.  The mime type is "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet".  When I try to open the document on my ASP.Net page I get the following error message.  "Excel found unreadable content in 'fileName.xlsx'.  Do you want to recover the contents of this workbook?  If you trust the source of this workbook, click Yes.".  When you click Yes I get the next error message of "Excel cannot open the file 'fileName.xlsx' because the file format or file extension is not valid.  Verify that the file has not been corrupted and that the file extension matched the format of the file.  This is a very basic Excel file that only has the date in cell 1, there are no pivot tables or anything else.  Here is the code that I am using to open the file.   If I save the file as Excel 97-2000 format it works fine.  But in Excel 2007 and 2010 it does not work.
	Response.AppendHeader("content-disposition", String.Format("attachment, filename={0}", "fileName"));

	Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

	Response.Buffer = true;
	Response.Charset = "";

	byte[] myFile = FileData;

	Response.OutputStream.Write(myFile, 0, myFile.Length);

catch (Exception ex)
	throw new Exception(ex.Message);


Open in new window

Question by:scott0912
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    Why are you calling a write operation twice?

    Accepted Solution

    I found the problem.  I was doing a BinaryWrite for PDF files and once I removed that then it started to work.
    LVL 50

    Expert Comment

    This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    Improved? Move/Copy Add-in Replacement - How to avoid the annoying, “A formula or sheet you want to move or copy contains the name XXX, which already exists on the destination worksheet.” David Miller (dlmille)  It was one of those days… I wa…
    Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
    Viewers will learn the basics of slicers and timelines for both PivotTables and standard Excel tables in Excel 2013.
    This Micro Tutorial will demonstrate how to use longer labels with horizontal bar charts instead of the vertical column chart.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now