private static object SetValueToModelHasMsg(IRow row, Dictionary<int, string> columnDictionary) { dynamic d = new System.Dynamic.ExpandoObject(); var model = d as ICollection<KeyValuePair<string, object>>; //创建属性,并赋值。 foreach (var dicConfig in columnDictionary) { try { if (string.IsNullOrEmpty(dicConfig.Value)) { continue; }
var cell = row.Cells.FirstOrDefault(a => a.ColumnIndex == dicConfig.Key); if (cell != null) { string value = GetValueByCell(cell); if (string.IsNullOrEmpty(value)) value = string.Empty; var item = model.FirstOrDefault(a => a.Key == dicConfig.Value); if (default(KeyValuePair<string, object>).Equals(item) == false) { //字段已经存在,覆盖 model.Remove(item); } model.Add(new KeyValuePair<string, object>(dicConfig.Value, value)); } } catch (Exception ex) { var errItem = model.FirstOrDefault(a => a.Key == "Msg"); if (default(KeyValuePair<string, object>).Equals(errItem)) { model.Add(new KeyValuePair<string, object>("Msg", ex.Message)); } }