private void ApplyMappingInfoAndValidate(ExcelSheet sheet) { // Map columns according to mapping list foreach (ImportFieldMapping mapping in MappingList) { if (!string.IsNullOrEmpty(mapping.ColumnName)) { ExcelSheetColumn column = sheet.Columns[mapping.ColumnName]; column.TargetName = mapping.RequestField; column.Validator = mapping.Validator; } } // Clear all row-level validate results foreach (ExcelSheetRow row in sheet.Rows) { row.RowLevelValidateResult.Clear(); } // Validate whole sheet // Trigger SheetValidate event ExcelSheetValidatingEventArgs arg = new ExcelSheetValidatingEventArgs(sheet); if (ExcelSheetValidating != null) { ExcelSheetValidating(this, arg); } if (!arg.IsCancelled) { // Validate each row for (int index = sheet.DataStartRowIndex; index < sheet.Rows.Count; index++) { ExcelSheetRow row = sheet.Rows[index]; // trigger RowValidate event ExcelSheetRowValidatingEventArgs args = new ExcelSheetRowValidatingEventArgs(row); if (ExcelSheetRowValidating != null) { ExcelSheetRowValidating(this, args); } if (!args.IsCancelled) { // Validate each cell in current row foreach (ExcelSheetCell cell in row.Cells) { cell.Validate(); } } } } }
/// <summary> /// Init the ImportMappingList according to ImportedData /// </summary> private void AutoMatchFields() { // Create dictionary of colums using uppercases Dictionary <string, ExcelSheetColumn> dic = new Dictionary <string, ExcelSheetColumn>(); foreach (ExcelSheetColumn column in CurrentSelectedSheet.Columns) { dic.Add(column.Name.ToUpper(), column); } // Do matching and mapping foreach (ImportFieldMapping mapping in MappingList) { string requestField = mapping.RequestField.ToUpper(); if (dic.ContainsKey(requestField)) { ExcelSheetColumn column = dic[requestField]; mapping.ColumnName = column.Name; } else { mapping.ColumnName = null; } } }