/// <summary> /// Try to parse the cell by the columns format type. /// </summary> /// <param name="workSheet"></param> /// <param name="rowNumber"></param> /// <param name="coltemplate"></param> /// <param name="cell"></param> /// <remarks>Will throw <see cref="NullReferenceException"/> errors. disable them when debugging through this dll file. </remarks> private void ParseCell(ExcelWorksheet workSheet, int rowNumber, ImportColumnTemplate coltemplate, ExcelRange cell) { switch (coltemplate.Column.Format) { case FormatType.Bool: { try { //try to parse bool value = _parser.ParseBool(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Currency: { try { decimal?value = _parser.ParseCurrency(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Date: { try { DateTime?date = _parser.ParseDate(cell); SetValue(workSheet, rowNumber, coltemplate, cell, date); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Duration: { try { TimeSpan?duration = _parser.ParseTimeSpan(cell); SetValue(workSheet, rowNumber, coltemplate, cell, duration); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Percent: { try { decimal?value = _parser.ParsePercent(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Double: { try { double?value = _parser.ParseDouble(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Decimal: { try { decimal?value = _parser.ParseDecimal(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.DecimalList: { try { decimal?val = null; try { val = _parser.ParseDecimal(cell); } catch (NullReferenceException) { //Fine if this is null. Lets keep going } // get info about property PropertyInfo modelPropertyInfo = _model.GetType().GetProperty(coltemplate.Column.ModelProperty); var list = modelPropertyInfo.GetValue(_model); Dictionary <string, decimal?> decimalList; if (list == null) //this is the first item in the list so we need to init the list. { decimalList = new Dictionary <string, decimal?>(); } else { decimalList = (Dictionary <string, decimal?>)list; } decimalList.Add(coltemplate.ColumnHeaderOptions[0], val); modelPropertyInfo.SetValue(_model, decimalList, null); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.Int: { try { int?value = _parser.ParseInt(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.StringList: { try { _parser.CheckIfCellIsNullOrEmpty(cell, "a string list"); string cellVal = cell.Value.ToString().Trim(); if (string.IsNullOrEmpty(cellVal) && !string.IsNullOrEmpty(cell.Formula)) { //We have a formula which is setting the field to blank instead of null throw new NullReferenceException(); } // get info about property PropertyInfo modelPropertyInfo = _model.GetType().GetProperty(coltemplate.Column.ModelProperty); object list = modelPropertyInfo.GetValue(_model); Dictionary <string, string> stringlist; if (list == null) //this is the first item in the list so we need to init the list. { stringlist = new Dictionary <string, string>(); } else { stringlist = (Dictionary <string, string>)list; } stringlist.Add(coltemplate.ColumnHeaderOptions[0], cellVal); modelPropertyInfo.SetValue(_model, stringlist, null); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } case FormatType.String: //uses default default: { try { string value = _parser.ParseString(cell); SetValue(workSheet, rowNumber, coltemplate, cell, value); } catch (NullReferenceException) { _singleRowErrors.Add(_excelExtensions.LogNullReferenceException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } catch (Exception) { _singleRowErrors.Add(_excelExtensions.LogCellException(workSheet.Name, coltemplate, cell.Address, rowNumber)); } break; } } }