Exception reading binary XLS file

Feb 4, 2013 at 1:43 PM
Hi all,

I just updated to the latest version ( and now I'm getting an exception when trying to read a XLS file. I was using version before without any problems.

I pass a MemoryStream to the CreateBinaryReader function and get an out-of-range exception (I've checked the stream and it's filled properly):

System.ArgumentOutOfRangeException : MemoryStream length must be non-negative and less than 2^31 - 1 - origin.
Parameter name: offset
at System.IO.MemoryStream.Seek(Int64 offset, SeekOrigin loc)
at Excel.Core.BinaryFormat.XlsFat..ctor(XlsHeader hdr, List`1 sectors, Int32 sizeOfSector, Boolean isMini, XlsRootDirectory rootDir)
at Excel.Core.BinaryFormat.XlsHeader.GetMiniFAT(XlsRootDirectory rootDir)
at Excel.Core.BinaryFormat.XlsStream.CalculateMiniFat(XlsRootDirectory rootDir)
at Excel.Core.BinaryFormat.XlsStream..ctor(XlsHeader hdr, UInt32 startSector, Boolean isMini, XlsRootDirectory rootDir)
at Excel.Core.BinaryFormat.XlsRootDirectory..ctor(XlsHeader hdr)
at Excel.ExcelBinaryReader.readWorkBookGlobals()
at Excel.ExcelBinaryReader.Initialize(Stream fileStream)
at Excel.ExcelReaderFactory.CreateBinaryReader(Stream fileStream)

Can anybody help me with this issue?


Feb 4, 2013 at 2:26 PM
I can help if you send an Excel doc to me and a snippet of code that shows this behaviour.
Feb 4, 2013 at 7:52 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 4, 2013 at 8:56 PM
I can't see what is going on here, something to do with the wrong stream type being used. Not sure if this is the library or a general .Net issue.

The following code shows a workaround, not sure why the way you are doing this does not work though.
        public void File_with_corrupt_or_missing_internal_FAT_structure_can_be_opened_without_exception_issue_11553()
            string filePath = @"D:\Projects\ExcelDataReader\Excel.Tests\Resources\Test_Issue_11553_FAT.xls";
            //var excelReader1 = ExcelReaderFactory.CreateBinaryReader(Helper.GetTestWorkbook("Test_Issue_11553_FAT"));

            var bytes2 = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite);
            var excelReader2 = ExcelReaderFactory.CreateBinaryReader(bytes2); // Works!

            var bytes = System.IO.File.ReadAllBytes(filePath);
            var excelReader3 = ExcelReaderFactory.CreateBinaryReader(new System.IO.MemoryStream(bytes)); // Does not work.
Feb 11, 2013 at 2:00 PM
i have the same problem in line

                    m_globals.CodePage = (XlsBiffSimpleValueRecord)rec;

                        m_encoding = Encoding.GetEncoding(m_globals.CodePage.Value);
                    catch (ArgumentException)
                        // Warning - Password protection
                        // TODO: Attach to ILog
m_globals.CodePage.Value 27651

After I covnverted the same file to XLSX and back save as XLS
the code
m_globals.CodePage.Value 1200

and it works file

The code and data sample

Feb 13, 2013 at 10:52 PM
A fix for this issue has been commited. Can you re-test.

@moiseyo - your issue sounds like a different one though it does appear to be valid
Feb 13, 2013 at 10:56 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.