Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Exception message shown when ExcelDataReader closed is misleading #89

Open
khensunny opened this issue Mar 31, 2024 · 0 comments
Open

Comments

@khensunny
Copy link

Hi there. I have been debugging an issue with some excel sheet throwing an error when enumerating its rows. The error message says: Sheet "Sheet1" has no heading. However, the sheet in question does have a heading. Fast forward, I found the root cause of the issue and as you can see with the code below:

 private static void Main(string[] args)
 {
    // NOTE: sheet.ReadRows uses yields to return the rows and at this point the ExcelDataReader has already been closed
    // because of the use of `using` to open the ExcelImporter
     var rows = ReadSheet(); 
     Console.WriteLine(rows.Count());
 }

 private static IEnumerable<ExcelPolicyDetailsImportRow> ReadSheet()
 {
     using (var stream = File.OpenRead("testing_sheet.xlsx"))
     {
         using (var importer = new ExcelImporter(stream))
         {
             importer.Configuration.RegisterClassMap<PolicyDetailsImportMap>();
             var sheet = importer.ReadSheet();
             return sheet.ReadRows<ExcelPolicyDetailsImportRow>();
         }
     }
 }

The rows are being read after the ExcelImporter has already been closed/disposed. My suggestion is that since ExcelDataReader has property IsClosed to tell whether the reader has been disposed of, that can be used to determine which message to show in situations where ExcelDataReader.Read() returns false.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant