Error with ExcelReaderFactory.CreateBinaryReader(memoryStream).

Aug 27, 2013 at 10:50 PM
I'm sure this error was discussed earlier but I could not get the solution,so posting it again.

I'm trying to read an .xls file using the below code and I get the error:"

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)
at LM.GB.CD.CDM.Sharepoint.WebControls.CDMControls.ReadXlsToDS(Byte[] bytes, String fileName, String tableName)
at LM.GB.CD.CDM.Sharepoint.WebControls.CDMControls.UploadFile(Byte[] bytes, String fileName, String fileType)
at LM.GB.CD.CDM.Sharepoint.WebControls.HomeDetail.UploadButtonClick(Object sender, EventArgs e)
public static DataSet ReadXlsToDS(byte[] bytes, string fileName,string tableName)
        {     
            MemoryStream memoryStream = new MemoryStream();
            memoryStream.Write(bytes, 0, bytes.Length);
            string fileExtension = Path.GetExtension(fileName);
            IExcelDataReader excelReader = null;

            if (fileExtension == ".xls")
            {
                //Reading from a binary Excel file ('97-2003 format; *.xls)
                excelReader = ExcelReaderFactory.CreateBinaryReader(memoryStream);
            }
            else if (fileExtension == ".xlsx")
            {
                    excelReader = ExcelReaderFactory.CreateOpenXmlReader(memoryStream);               
            }
Sep 10, 2013 at 9:12 PM
Edited Sep 10, 2013 at 9:35 PM
I had this same problem.

It was indeed fix, but the version of Excel Data Reader that is distributed through NuGet ( labeled 2.1.1 ) does not have the fix in it (Although, based on the date it was last updated, it should include the fix ).

I downloaded the source from codeplex, compiled it on VS2012, and double checked that the Unit Test for the MemoryStream source bug CreateBinaryReader did indeed pass. I later copied this version of the DLL to my project, and my existing code that was failing, began to work.

Long story short, there's some versioning inconsitency with some of the Excel Data Reader binaries.

The test method for this in the Unit Test is "Issue_11553_11570_FATIssue_Offset", which calls DoTestFATStreamIssue in which a MemoryStream is created from a byte array and passed into the Binary Reader successfully.

EDIT:
So, I just went to nuget, and rolled back from 2.1.1, to 2.1, and the MemoryStream issue was fixed.
Something is wrong with the NuGet 2.1.1 version. I notified the NuGet owners of the problem.

EDIT 2:
Just FYI, if you go down from 2.1.1 to 2.1, then excel OLE Automation dates are considered strings. In 2.1.1, the OA dates are considered double's.