Skip to content

Commit

Permalink
Use sheet iterator (fixes #96)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Ganss committed Jan 15, 2021
1 parent e9bcc1f commit 848ebbd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
14 changes: 14 additions & 0 deletions ExcelMapper.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1892,5 +1892,19 @@ public void ColumnSkipTest()
Assert.AreEqual("Nudossi", p.Name);
Assert.AreEqual(60, p.Number);
}

class NullProduct
{
public string Number { get; set; }
public string Color { get; set; }
}

[Test]
public void NullTest()
{
// see https://github.com/mganss/ExcelMapper/issues/96
var products = new ExcelMapper(@"..\..\..\null_test.xlsx").Fetch<NullProduct>().ToList();
Assert.AreEqual(20, products.Count);
}
}
}
Binary file added ExcelMapper.Tests/null_test.xlsx
Binary file not shown.
11 changes: 6 additions & 5 deletions ExcelMapper/ExcelMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -410,15 +410,18 @@ IEnumerable Fetch(ISheet sheet, Type type, Func<string, object, object> valuePar
})
.Where(c => c.ColumnInfo?.Any() ?? false)
.ToDictionary(c => c.ColumnIndex, c => c.ColumnInfo);
var i = MinRowNumber;
IRow row = null;

if (TrackObjects) Objects[sheet.SheetName] = new Dictionary<int, object>();

var objInstanceIdx = 0;

while (i <= MaxRowNumber && (row = sheet.GetRow(i)) != null)
foreach (IRow row in sheet)
{
var i = row.RowNum;

if (i < MinRowNumber) continue;
if (i > MaxRowNumber) break;

// optionally skip header row and blank rows
if ((!HeaderRow || i != HeaderRowNumber) && (!SkipBlankRows || row.Cells.Any(c => !IsCellBlank(c))))
{
Expand Down Expand Up @@ -458,8 +461,6 @@ IEnumerable Fetch(ISheet sheet, Type type, Func<string, object, object> valuePar

yield return o;
}

i++;
}
}

Expand Down

0 comments on commit 848ebbd

Please sign in to comment.