0

I am using ExcelDataReader library for read excel files in .Net project. When I read ".xls" with CreateBinaryReader everything works fine. But, when I read ".xlsx" with CreateOpenXmlReader throwing "object reference not set to an instance of an object" exception.

    using (FileStream excelfile = System.IO.File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read))
    {
    IExcelDataReader excelReader;
    string ftype = Path.GetExtension(excelfile.Name);
    if(ftype == “.xls")
       excelReader = ExcelReaderFactory.CreateBinaryReader(excelfile,true);
    else
       excelReader = ExcelReaderFactory.CreateOpenXmlReader(excelfile);
    ...
    }

My ExcelDataReader version is 2.1.2.3 and I can't update. I followed it with debug, but I couldn't detect the problem. What should I do about this problem?

Solved: I had to upgrade and when I edited the changed functions, I read without errors in both file types. I use "CreateReader" function.

AlpYuktug
  • 178
  • 1
  • 2
  • 13
  • Not to the point, but you seem to declare a `string ftype` and in the if statement you check for `type` variable. Typo? – SnowGroomer Jun 17 '22 at 09:24
  • @SnowGroomer Yes, I made a typo. The spelling in the original code is correct. – AlpYuktug Jun 17 '22 at 10:22
  • `ExcelReaderFactory.Create` will create the proper reader based on the extension. You don't have to check the extension yourself. You didn't include the actual exception in the question or even showed where it occured, so it's impossible to help right now. Perhaps the file is broken? Or perhaps you try to access some variable that's null? Perhaps the extension isn't what you expected so `excelReader` is `null` when you try to use it? – Panagiotis Kanavos Jun 17 '22 at 10:56
  • `My ExcelDataReader version is 2.1.2.3 and I can't update.` why? That's an 8 year old version. If the NRE is caused by a bug, the fix is in the newer versions. – Panagiotis Kanavos Jun 17 '22 at 10:58
  • Actually, I use same excel. Just someone .xls version someone .xlsx @PanagiotisKanavos – AlpYuktug Jun 17 '22 at 11:41
  • I had to make multiple changes when I upgraded. But I had to upgrade and when I edited the changed functions, I read without errors in both file types. @PanagiotisKanavos – AlpYuktug Jun 17 '22 at 11:42

0 Answers0