MickJuice
MickJuice

Reputation: 539

Cannot read XLSX file - Exception with Stream "Cannot access Closed File

I am having trouble reading from an XLSX file (no trouble at all with XLS files) and I believe it has to do with the FileStream object. Here's my code:

string extention = Path.GetExtension(fileName);
        using (FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read))
        {
            IExcelDataReader excelReader = null;

            try
            {
                switch (extention.ToLower())
                {
                    case ".xls":
                        excelReader = ExcelReaderFactory.CreateBinaryReader(stream);
                        break;
                    case ".xlsx":
                        excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);
                        break;

               ....

Again, when I read from an XLS file, it works, however for XLSX files, when I dig into the stream variable, I see the following

Handle = 'stream.Handle' threw an exception of type 'System.ObjectDisposedException'

Length = 'stream.Length' threw an exception of type 'System.ObjectDisposedException'

....

With the same exception for other members of FileStream. When I dig further into these exceptions, I see:

base {System.InvalidOperationException} = {"Cannot access a closed file."}

As the message. What does this mean? I'm assuming this is preventing me from reading from this file. Is there any way around this? Why would it work for XLS but not XLSX files?

Edit for Eric:

No dice. I'm getting a "Cannot Access a Closed Stream" exception on the MemoryStream object. I'm setting up the MemoryStream as follows (per [http://justtwoshare.blogspot.com/2010/04/how-to-convert-filestream-to.html](this site):

FileStream stream = File.Open(fileName, FileMode.Open, FileAccess.Read);
        MemoryStream memStream = new MemoryStream();
        memStream.SetLength(stream.Length);
        stream.Read(memStream.GetBuffer(), 0, (int)stream.Length);
        memStream.Flush();

Final Edit:

After having no luck with finding out what the issue with this was, I resorted to limiting the program to only accepting xls files...

Upvotes: 0

Views: 3332

Answers (3)

krokador
krokador

Reputation: 69

I realize this is super old, but as it turns out I just had the same issue in a project and have had to work around the fact that ExcelReaderFactory.CreateOpenXmlReader(stream); automatically closes the stream. Since I need to do work on that same stream further down the code, I just basically re-initalized the stream to an open state after the method call.

stream = New MemoryStream(stream.ToArray());

As I have found in another thread on here (MemoryStream, Cannot access a closed stream), calling ToArray after the stream is closed doesn't cause any issues, and then I have a fresh new, open stream to work with.

Hope that can help anyone stumbling on here in the future!

Upvotes: 1

Eric Petroelje
Eric Petroelje

Reputation: 60498

I've used that same library before - try omitting the using block around the FileStream you create. The ExcelReader should take care of closing and disposing of the stream for you.

Another alternative would be to read to the end of the FileStream putting the results in a MemoryStream. Then pass the MemoryStream into the ExcelReader. That way you can dispose of the FileStream and don't have to worry about the MemoryStream being disposed of properly.

Upvotes: 2

Haney
Haney

Reputation: 34802

The difference is probably in the method:

excelReader = ExcelReaderFactory.CreateOpenXmlReader(stream);

Can you show us that code and verify that you're not accidentally prematurely closing the stream?

Upvotes: 0

Related Questions