0

I am trying to import an excel file (xlsx) but it throws the following question :

Could not load file or assembly 'ICSharpCode.SharpZipLib, Version=0.86.0.518, Culture=neutral, PublicKeyToken=1b03e6acf1164f73' or one of its dependencies. The system cannot find the file specified.

But it works perfectly fine for xls with the same code.

  using (FileStream file = new FileStream(filename, FileMode.Open, 
      FileAccess.Read))
                {
                    FileExtension = Path.GetExtension(filename);
                    if 
   (FileExtension.ToLower().Equals(StringConstants.FILE_EXTENSION_XLS))
                    {
                        HSSFWorkBook = new HSSFWorkbook(file);
                        intCountSheets = HSSFWorkBook.NumberOfSheets;
                        EaRepos = DiagFunAnaClass.EaRepos;
                        objPackage = DiagFunAnaClass.objPackage;
                        plantCode = DiagFunAnaClass.plantCode;
                        buttonValidate.Enabled = true;
                    }
                    else if 
    (FileExtension.ToLower().Equals(StringConstants.FILE_EXTENSION_XLSX))
                    {
                        XSSFWorkBook = new XSSFWorkbook(file);

The error comes in the last line of the above code.

Shrikant
  • 523
  • 4
  • 15

2 Answers2

1

I tried below code and it is not throwing any exception:

 string filename = @"D:\test.xlsx";
            using (FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read))
            {
                string FileExtension = Path.GetExtension(filename);
                if (FileExtension.ToLower().Equals(".xls"))
                {
                    var HSSFWorkBook = new HSSFWorkbook(file);
                    int CountSheets = HSSFWorkBook.NumberOfSheets;
                }
                else if (FileExtension.ToLower().Equals(".xlsx"))
                {
                    var XSSFWorkBook = new XSSFWorkbook(file);
                }
            }

I think there may be two reasons for the error you encounter:

  1. The file you are trying to access is open. You should close the file from all the editors and try again.
  2. The version of NPOI that I am using is 2.3.0. You may be using older version.
Manprit Singh Sahota
  • 1,279
  • 2
  • 14
  • 37
0

your code seems to be correct and I also tried the Manprit's code. It is working correctly in my version 2.2.1. Sometime in NPOI functionality doesn't work even you are following everything as per documentation. Last thing I can suggest you is try typecasting the filestream type for XSSF with Stream type like below

var XSSFWorkBook = new XSSFWorkbook((Stream)file);

FileStream is derived from Stream class

kumar chandraketu
  • 2,232
  • 2
  • 20
  • 25