Skip to content

Commit

Permalink
Merge pull request #28 from IowaComputerGurus/feature/corrupt-data
Browse files Browse the repository at this point in the history
Corrupt Data Import
  • Loading branch information
mitchelsellers authored May 9, 2022
2 parents 52394a5 + d296ac5 commit 61f5c19
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/NetCore.Utilities.Spreadsheet/OpenXmlSpreadsheetParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,23 @@ public class OpenXmlSpreadsheetParser : ISpreadsheetParser
var wsPart = workbookPart.GetPartById(sheet.Id) as WorksheetPart;
var collection = new Collection<T>();
var skipRows = skipHeaderRow ? 1 : 0;
var expectedColumns = importColumnDefinitions.Max(c => c.Column) - 1;
foreach (var row in wsPart.Worksheet.Descendants<Row>().Skip(skipRows))
{
var tnew = new T();
var cellCollection = row.Elements<Cell>().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);
Expand Down

0 comments on commit 61f5c19

Please sign in to comment.