/// <summary> /// 根据数据设置对象属性值 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="context"></param> /// <param name="customLog"></param> /// <param name="objItem"></param> /// <returns></returns> public static bool SetPropertiesValue <T>(ExcportRowContext context, StringBuilder customLog, T objItem) { bool result = false; Dictionary <string, PropertyInfo> properties = TypePropertiesCacheQueue.Instance.GetPropertyDictionary(typeof(T)); foreach (ExportCellDescription cellDesp in context.PropertyDescriptions) { if (properties.ContainsKey(cellDesp.PropertyName)) { PropertyInfo propertyInfo = properties[cellDesp.PropertyName]; try { if (cellDesp.Value != null) { propertyInfo.SetValue(objItem, DataConverter.ChangeType(cellDesp.Value, propertyInfo.PropertyType), null); } } catch (Exception ex) { customLog.AppendFormat("{0}单元格值:{1},不能转换到指定对象{2}属性!", cellDesp.Address, cellDesp.Value.ToString(), cellDesp.PropertyName); customLog.AppendFormat("错误信息{0}", ex.ToString()); result = false; } } } return(result); }
/// <summary> /// 将Excel数据直充到指定的Collection中 /// </summary> /// <typeparam name="T">数据模型</typeparam> /// <typeparam name="TCollection">待待充Collection 必须实现ICollection<T> 接口</typeparam> /// <param name="collection">待待充Collection</param> /// <param name="tbDesp">Table相关描述信息</param> /// <param name="param">填充时所准备的相关参数,包括初始化每一个对象委托,根据相关数据设置相关属性值委托(默认反射)</param> /// <returns></returns> public string GetCollectionFromTable <T, TCollection>(TCollection collection, TableDescription tbDesp, SpreadGetTableCollectionParams <T> param) where TCollection : ICollection <T> { tbDesp.NullCheck("tbDesp"); collection.NullCheck("数据集合不能为空"); param.ExportRow.NullCheck("exportRow"); this.CheckTableExists(tbDesp.TableName); StringBuilder customLog = new StringBuilder(); Table tb = this.Tables[tbDesp.TableName]; int rowIndex = 0; foreach (TableRow tr in tb.Rows) { ExcportRowContext context = new ExcportRowContext(); context.RowIndex = rowIndex; foreach (TableColumnDescription tc in tbDesp.AllColumns) { if (tb.Columns.ContainsKey(tc.ColumnName)) { context.PropertyDescriptions.Add(new ExportCellDescription(tc.PropertyName) { TableColumnName = tc.ColumnName, Value = tr[tb.Columns[tc.ColumnName]].Value, Address = CellAddress.Parse(tb.Columns[tc.ColumnName].Position + tb.Address.StartColumn, tr.RowIndex + tb.Address.StartRow + 1).ToString() }); } } ExportRowResult <T> result = param.ExportRow(context); if (result.Validated == true && result.CurrentObject != null) { collection.Add(result.CurrentObject); } customLog.Append(result.ErrorLog); rowIndex++; if (result.Validated == false && param.ValidationOperator == ValidationErrorStopMode.Stop) { break; } } return(customLog.ToString()); }
/// <summary> /// 逐行获取Table数据 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="tbDesp"></param> /// <param name="getemptyObj"></param> /// <param name="validationOperator"></param> public string ForEachTableRows <T>(TableDescription tbDesp, SpreadGetTableCollectionParams <T> param, out int upCount) { tbDesp.NullCheck("tbDesp"); param.ExportRow.NullCheck("exportRow"); this.CheckTableExists(tbDesp.TableName); StringBuilder customLog = new StringBuilder(); Table tb = this.Tables[tbDesp.TableName]; int rowIndex = 0; upCount = 0; foreach (TableRow tr in tb.Rows) { ExcportRowContext context = new ExcportRowContext() { RowIndex = rowIndex }; foreach (TableColumnDescription tc in tbDesp.AllColumns) { if (tb.Columns.ContainsKey(tc.ColumnName)) { context.PropertyDescriptions.Add(new ExportCellDescription(tc.PropertyName) { TableColumnName = tc.ColumnName, Value = tr[tb.Columns[tc.ColumnName]].Value, Address = CellAddress.Parse(tb.Columns[tc.ColumnName].Position + tb.Address.StartColumn, tr.RowIndex + tb.Address.StartRow + 1).ToString() }); } } ExportRowResult <T> result = param.ExportRow(context); customLog.Append(result.ErrorLog); rowIndex++; if (result.Validated) { upCount++; } else if (param.ValidationOperator == ValidationErrorStopMode.Stop) { break; } } return(customLog.ToString()); }