Parcourir la source

Fixed empty file reading

Piotr Czajkowski il y a 3 jours
Parent
commit
2b9519040b

+ 3 - 1
ExcelORM/ExcelORM/ExcelDynamicReader.cs

@@ -54,7 +54,9 @@ public class ExcelDynamicReader : IDisposable
 
         var firstRow = worksheet.Row((int)startFrom);
         if (firstRow.IsEmpty())
-            firstRow = worksheet.RowsUsed().First(x => x.RowNumber() > startFrom && !x.IsEmpty());
+            firstRow = worksheet.RowsUsed().FirstOrDefault(x => x.RowNumber() > startFrom && !x.IsEmpty());
+        
+        if (firstRow == null || firstRow.IsEmpty()) yield break;
 
         var mapping = DynamicCell.MapHeader(firstRow.CellsUsed());
         if (mapping == null || mapping.Count == 0) yield break;

+ 10 - 0
ExcelORM/ExcelORMTests/DynamicReaderTests.cs

@@ -48,4 +48,14 @@ public class DynamicReaderTests
         Assert.NotEmpty(results);
         Assert.Equal(results.First().Count, results.Last().Count);
     }
+    
+    private const string EmptyFileForAppend = "testFiles/emptyForAppend.xlsx";
+    
+    [Fact]
+    public void ReadEmptyFile()
+    {
+        using var reader = new ExcelDynamicReader(EmptyFileForAppend);
+        var results = reader.Read().ToArray();
+        Assert.Empty(results);
+    } 
 }