Can't open xlsx file

Dec 15, 2011 at 8:02 PM

I am trying to do the most simplest thing- open an existing test.xlsx file but always fail on accessing package.Workbook.

FileInfo existingFile = new FileInfo("TestSpreadsheets\\test1.xlsx");
using (ExcelPackage package = new ExcelPackage(existingFile))
{
  ExcelWorkbook workbook = package.Workbook; // throws exception
}

"Index was out of range. Must be non-negative and less than the size of the collection.".  

 

The file does exist, I can see that under the package info, along with the size and various other things.

Dec 19, 2011 at 6:10 PM

You use the FileInfo object to check if the path is valid before attempting to load the ExcelPackage.  In my code I do not use the \\ escape character in my paths and it works just fine.  The only strange thing to keep in mind is that when referencing Worksheets(x) the index is not 0 based, it begins at 1.