We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. Image for the cookie policy date
close icon

Opening an Excel File using a stream

Hi, I am having a problem opening an .xls file using a stream as a parameter, it raises the following error: "Index was out of range. Must be non-negative and less than the size of the collection" the c# code is below: FileStream FS = new FileStream(@"c:\temp\test.xls", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); IWorkbook myWorkBook = ExcelUtils.Open(FS); IWorksheet sheet = myWorkBook.Worksheets[0]; after watching the myWorkBook interface the worksheet collection is empty, however when I open the worksheet using the code: IWorkbook myWorkBook = ExcelUtils.Open(@"c:\temp\test.xls"); it works as expected with no errors, can you explain the problem, as the help for ExcelRW is very limited, with no examples in this area. Also I noticed on the Forum, that reading from a .xls file which is open is not suppoted as yet, has this been fixed? Thanks Richard Howarth

3 Replies

AD Administrator Syncfusion Team August 26, 2004 11:06 AM UTC

Hi Richard, 1) Could you try adding this line FileStream FS = new FileStream("Template.xls", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); FS.Seek(0, SeekOrigin.Begin); IWorkbook myWorkbook = ExcelUtils.Open(FS); Also, make sure you are using version 2.1.0.9. If not please log on to Direc Trac to download the latest version of Essential ExcelRW. It has many enhancements over the previous versions. 2) An XLS file that is already opened can be opened using ExcelRW in the procedure mentioned above. Using FileStream to open the file in Read mode and then opening the filestream using ExcelRW. Please let me know if you have any questions. Thanks. Best regards, Stephen. >Hi, > >I am having a problem opening an .xls file using a stream as a parameter, it raises the following error: > >"Index was out of range. Must be non-negative and less than the size of the collection" > >the c# code is below: > >FileStream FS = new FileStream(@"c:\temp\test.xls", FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite); >IWorkbook myWorkBook = ExcelUtils.Open(FS); >IWorksheet sheet = myWorkBook.Worksheets[0]; > >after watching the myWorkBook interface the worksheet collection is empty, however when I open the worksheet using the code: > >IWorkbook myWorkBook = ExcelUtils.Open(@"c:\temp\test.xls"); > >it works as expected with no errors, can you explain the problem, as the help for ExcelRW is very limited, with no examples in this area. > >Also I noticed on the Forum, that reading from a .xls file which is open is not suppoted as yet, has this been fixed? > >Thanks >Richard Howarth


GA Geir Aamodt March 4, 2005 12:26 PM UTC

I''m also having problems with opening the Excel as an MemoryStream. Are receiving this Exception: "System.IndexOutOfRangeException : Index was outside the bounds of the array" when executing the "myWorkBook = ExcelUtils.Open(memStream);" statement (see below). Any clues on how to solve this? --------------- code snippet --------------- public void ImportProductData(byte [] excelFile) { IWorkbook myWorkBook=null; using (MemoryStream memStream = new MemoryStream(excelFile)) { memStream.Seek(0, SeekOrigin.Begin); myWorkBook = ExcelUtils.Open(memStream); // Create the worksheet object IWorksheet sheet = myWorkBook.Worksheets[0]; //..code deleted } } --------------- code snippet --------------- (The excelFile parameter contains a byte representation of the excel file and I have tested it to be 100% correct.) System information: I''m using ExcelRW.Base version 3.0.1.0 and are working on an Excel 2003 document. All on WinXP SP2 machine with .NET FW 1.1 Best regards, Geir


AD Administrator Syncfusion Team March 8, 2005 11:50 AM UTC

Hi Geir, Are you able to open the same file without any problem when you open it from file?, I just want to make sure that its not a problem opening that specific file since there are sutuations where some files can be opened using MS Excel and not using ExcelRW. Could you please create a Direc-Trac incident and send me the file that you are not able to open from memory stream. Thanks, Stephen. >I''m also having problems with opening the Excel as an MemoryStream. Are receiving this Exception: "System.IndexOutOfRangeException : Index was outside the bounds of the array" when executing the "myWorkBook = ExcelUtils.Open(memStream);" statement (see below). > >Any clues on how to solve this? > >--------------- code snippet --------------- >public void ImportProductData(byte [] excelFile) >{ > IWorkbook myWorkBook=null; > > using (MemoryStream memStream = new MemoryStream(excelFile)) > { > memStream.Seek(0, SeekOrigin.Begin); > myWorkBook = ExcelUtils.Open(memStream); > > // Create the worksheet object > IWorksheet sheet = myWorkBook.Worksheets[0]; > > //..code deleted > } >} >--------------- code snippet --------------- > >(The excelFile parameter contains a byte representation of the excel file and I have tested it to be 100% correct.) > >System information: >I''m using ExcelRW.Base version 3.0.1.0 and are working on an Excel 2003 document. All on WinXP SP2 machine with .NET FW 1.1 > >Best regards, > >Geir >

Loader.
Live Chat Icon For mobile
Up arrow icon