From d296ac511e02997e860a875433edff300ae132ae Mon Sep 17 00:00:00 2001 From: Kenneth Sellers Date: Sun, 8 May 2022 20:34:40 -0500 Subject: [PATCH] Closes #27, Addressing issues with rows not containing the number of columns expected by the import model. Skip any rows that don't have the expected number of columns. --- .../OpenXmlSpreadsheetParser.cs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/NetCore.Utilities.Spreadsheet/OpenXmlSpreadsheetParser.cs b/src/NetCore.Utilities.Spreadsheet/OpenXmlSpreadsheetParser.cs index df8f32c..f0d2c6f 100644 --- a/src/NetCore.Utilities.Spreadsheet/OpenXmlSpreadsheetParser.cs +++ b/src/NetCore.Utilities.Spreadsheet/OpenXmlSpreadsheetParser.cs @@ -51,13 +51,23 @@ public class OpenXmlSpreadsheetParser : ISpreadsheetParser var wsPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart; var collection = new Collection(); var skipRows = skipHeaderRow ? 1 : 0; + var expectedColumns = importColumnDefinitions.Max(c => c.Column) - 1; foreach (var row in wsPart.Worksheet.Descendants().Skip(skipRows)) { var tnew = new T(); var cellCollection = row.Elements().ToList(); + //Check to see if the row has at least the same number of cells as the import model expects. + //If not, skip the row + if (cellCollection.Count < expectedColumns) + continue; + foreach (var col in importColumnDefinitions) { + if (cellCollection.ElementAtOrDefault(col.Column - 1) == null) + { + continue; + } var value = GetCellValue(cellCollection[col.Column - 1]); if (string.IsNullOrEmpty(value)) col.Property.SetValue(tnew, null);