internal static void ToSheet <T>(Workbook workbook, List <T> dataList, string sheetName, List <string> filterNameList = null, bool propertyContain = true, Dictionary <string, object> propertyDict = null, Dictionary <string, object> valueFormatDict = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { Worksheet iSheet = workbook.Worksheets[0]; iSheet.Name = sheetName; // 获得表头数据 Dictionary <string, PropertyInfo> headerColumnNameDict = CommonHelper.InitPropertyWriteMapperFormat <T, ExcelTAttribute>(propertyDict, filterNameList, propertyContain); Cells cellList = iSheet.Cells; int columnIndex = 0; // 遍历设置表头 foreach (KeyValuePair <string, PropertyInfo> keyValuePair in headerColumnNameDict) { cellList[0, columnIndex].PutValue(keyValuePair.Key); columnIndex++; } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } if (dataList != null) { // 遍历设置数据 for (int rowIndex = 1; rowIndex <= dataList.Count; rowIndex++) { SetRowDataValue(cellList, rowIndex, dataList[rowIndex - 1], propertyGetDict, headerColumnNameDict, valueFormatDict); } } }
internal static void ToSheet <T>(IWorkbook workbook, List <T> dataList, string sheetName, Action <ICell, object> cellCallback = null, Action <ISheet, List <string> > sheetCallback = null, bool isHeader = true, List <string> filterNameList = null, bool propertyContain = true, Dictionary <string, object> propertyDict = null, Dictionary <string, object> valueFormatDict = null, ReflectionTypeEnum reflectionType = ReflectionTypeEnum.Expression) where T : class, new() { ISheet iSheet = workbook.CreateSheet(sheetName); // 获得表头数据 Dictionary <string, PropertyInfo> headerColumnNameDict = CommonHelper.InitPropertyWriteMapperFormat <T, ExcelTAttribute>(propertyDict, filterNameList, propertyContain); int dataIndex = 0; if (isHeader) { IRow headerRow = iSheet.CreateRow(dataIndex); int columnIndex = 0; // 遍历设置表头 foreach (KeyValuePair <string, PropertyInfo> keyValuePair in headerColumnNameDict) { headerRow.CreateCell(columnIndex).SetCellValue(keyValuePair.Key); columnIndex++; } dataIndex = 1; } dynamic propertyGetDict = null; if (reflectionType != ReflectionTypeEnum.Original) { propertyGetDict = ReflectionExtendHelper.PropertyGetCallDict <T>(reflectionType); } IRow row = null; if (dataList != null) { // 遍历设置数据 for (int rowIndex = 0; rowIndex < dataList.Count; rowIndex++, dataIndex++) { row = iSheet.CreateRow(dataIndex); SetRowDataValue(row, cellCallback, dataList[rowIndex], propertyGetDict, headerColumnNameDict, valueFormatDict); } } if (sheetCallback != null) { sheetCallback(iSheet, headerColumnNameDict.Keys.ToList()); } }