/// <summary> /// 填充除了表头之外每一行数据行,根据给定属性名称清单 /// </summary> /// <param name="list"></param> /// <param name="worksheet"></param> /// <param name="rowHeads"></param> private static void FillRows(IEnumerable <T> list, ExcelWorksheet worksheet, List <RowTitleInfo> rowHeads) { var rowIndex = 2; var propertyList = (from row in rowHeads select row.AttrName).ToList(); var entities = list as T[] ?? list.ToArray(); var keyTypeDic = ReflectPropertyUtil <T> .ReflectGetPropertyKeyTypePairs(entities.FirstOrDefault(), propertyList); foreach (var keyValueDic in entities.Select(t => ReflectPropertyUtil <T> .ReflectGetPropertyKeyValuePairs(t, propertyList))) { FillCells(keyValueDic, keyTypeDic, rowHeads, worksheet, rowIndex); rowIndex++; } }
/// <summary> /// 填充worksheet,根据Entity自身属性名称清单 /// </summary> /// <param name="worksheet"></param> /// <param name="list"></param> /// <returns></returns> public static bool FillWorksheet(ExcelWorksheet worksheet, IEnumerable <T> list) { var entities = list as T[] ?? list.ToArray(); var keyTypeDic = ReflectPropertyUtil <T> .ReflectGetPropertyKeyTypePairs(entities.FirstOrDefault()); var rowHeads = (from typeCode in keyTypeDic select typeCode.Key).ToList(); var result = FillHead(worksheet, rowHeads); if (!result) { Logger.Info("填充列头信息失败"); return(false); } FillRows(entities, worksheet, keyTypeDic, rowHeads); return(true); }