5

I am getting 0 worksheet count when the excel file (.xls) has 4 worksheets. I dont know why its not working for me. Please help.

Excel file last in Excel Version 1908 Office 365
EPPlus Version : 4.5.3.3
C# .Net Framework Version : 4.7

public static void readXLS(string FilePath)
{
    FileInfo existingFile = new FileInfo(FilePath);
    using (ExcelPackage package = new ExcelPackage(existingFile))
    {
        int count = package.Workbook.Worksheets.Count;
    }
}

I am sharing the excel file I want to read, here is the link

  • nothing seems wrong with your code, maybe check if [this](https://stackoverflow.com/questions/29775107/worksheet-position-out-of-range-connection-closed-when-using-epplus) relates, do you have named ranges in your sheets? – jalsh Feb 27 '20 at 03:16
  • I did have named ranges,so i deleted them. still it was not able to get the worksheets. I checked the link, so I created a new excel file and then it was able to read that new excel file. But it wont read existing excel file. – Shriram Karpur Feb 27 '20 at 03:49
  • I think there're still traces of your named ranges hidden in the meta-data in the file – jalsh Feb 27 '20 at 03:55
  • I am sharing the excel file I want to read, here is the link https://drive.google.com/open?id=1ulBXtfdtwGPY-VM1zojdkkMdwDo1lo1G – Shriram Karpur Feb 27 '20 at 04:09
  • I dont know how to look at the metadata in excel file for hidden named ranges. And is it known that if there are named ranges, EPPlus cannot read an excel file ? – Shriram Karpur Feb 27 '20 at 04:10
  • I can confirm that the problem is still existing with .NET Core 2.2 and EPPlust.Core 1.5.4 – Annosz Mar 06 '20 at 15:19

2 Answers2

0

It doesn't look like it was your case but it may happen to other people so I'll leave it as an answer but in my case the file extension was .xlsx so no issue on this side. However, the file was located on a network location (even if I was accessing it via a network drive defined in Windows).

So if you face this issue, you might want to have your code to copy the file first to a temporary local destination, and then access it from there with EPPlus. Once you're done you can clean after self the local copy.

In my case credentials to access the network location were also different from those I use in Windows so your mileage may vary a bit based on this too.

Guillaume
  • 1,782
  • 1
  • 25
  • 42
-1

I figured out that EPPlus works fine if the file is saved as in extension .xlsx I am fine using .xlsx, so for me this is not an issue anymore.