diff --git a/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs b/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs index 3df92cf..3bb4c3d 100644 --- a/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs +++ b/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.Data; using System.IO; using System.Linq; using System.Linq.Dynamic.Core; @@ -269,75 +270,117 @@ public class ZbxBase ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase) { + var isHasDetail = false;//是否包含从表 var data= (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items; var fileStream = new MemoryStream(); IWorkbook workbook = new XSSFWorkbook(); - ISheet sheet = workbook.CreateSheet("Sheet1"); + ISheet sheet = workbook.CreateSheet(typeof(TEntityDto).Name); - // 获取主表和从表的属性 + // 获取主表的属性 创建主表 表头 var mainProperties = typeof(TEntityDto).GetProperties(); - var detailProperties = typeof(TEntityDto).GetProperty("Details").PropertyType.GetGenericArguments()[0].GetProperties(); - - // 创建表头 IRow headerRow = sheet.CreateRow(0); for (int i = 0; i < mainProperties.Length; i++) { headerRow.CreateCell(i).SetCellValue(mainProperties[i].Name); } - for (int i = 0; i < detailProperties.Length; i++) + + // 获取从表的属性 创建从表 表头 + var detailProperties = typeof(TEntityDto).GetProperty("Details")?.PropertyType.GetGenericArguments()[0].GetProperties(); + if (detailProperties != null) { - headerRow.CreateCell(mainProperties.Length + i).SetCellValue(detailProperties[i].Name); + isHasDetail = true; + for (int i = 0; i < detailProperties.Length; i++) + { + headerRow.CreateCell(mainProperties.Length + i).SetCellValue(detailProperties[i].Name); + } } // 填充数据行 int rowIndex = 1; foreach (var mainDto in data) { - IRow dataRow = sheet.CreateRow(rowIndex); - - // 填充主表数据 - for (int i = 0; i < mainProperties.Length; i++) - { - var value = mainProperties[i].GetValue(mainDto); - dataRow.CreateCell(i).SetCellValue(value?.ToString()); - } - - // 填充从表数据 - var detailList = (IEnumerable)mainProperties[0].GetValue(mainDto); - int detailCellIndex = mainProperties.Length; - foreach (var detailItem in detailList) + if (isHasDetail) { - IRow detailRow; - if (rowIndex == 1) + // 获取从表数据 + var detailsIndex = mainProperties.FindIndex(p => p.Name == "Details"); + //从表 + var detailList = (IEnumerable)mainProperties[detailsIndex].GetValue(mainDto); + var startMainRowIndex = rowIndex; + for (int datailCount = 0; datailCount < detailList.Count(); datailCount++) { - // 创建新的行用于显示从表数据 - detailRow = sheet.CreateRow(detailCellIndex); + IRow dataRow = sheet.CreateRow(rowIndex); + + // 填充主表数据 + for (int i = 0; i < mainProperties.Length; i++) + { + var value = mainProperties[i].GetValue(mainDto); + dataRow.CreateCell(i).SetCellValue(value?.ToString()); + } + + rowIndex++; } - else + var overMainRowIndex = rowIndex; + foreach (var detail in detailList) { - // 获取已创建的行用于显示从表数据 - detailRow = sheet.GetRow(detailCellIndex); - } + if (startMainRowIndex <= overMainRowIndex) + { + var detailRow = sheet.GetRow(startMainRowIndex); + for (int i = 0; i < detailProperties.Length; i++) + { + var value = detailProperties[i].GetValue(detail); + detailRow.CreateCell(mainProperties.Length + i).SetCellValue(value?.ToString()); + } + } - for (int i = 0; i < detailProperties.Length; i++) + startMainRowIndex++; + } + } + else + { + IRow dataRow = sheet.CreateRow(rowIndex); + // 填充主表数据 + for (int i = 0; i < mainProperties.Length; i++) { - var value = detailProperties[i].GetValue(detailItem); - detailRow.CreateCell(mainProperties.Length + i).SetCellValue(value?.ToString()); + var value = mainProperties[i].GetValue(mainDto); + dataRow.CreateCell(i).SetCellValue(value?.ToString()); + + var CellStyle = workbook.CreateCellStyle(); + CellStyle.FillBackgroundColor = IndexedColors.DarkRed.Index; + CellStyle.FillPattern = FillPattern.SolidForeground; + + dataRow.GetCell(i).CellStyle= CellStyle; } - - detailCellIndex++; } + //添加1个空行将2条数据分割开 rowIndex++; } - // 自动调整列宽 - for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++) + if (isHasDetail) + { + // 自动调整列宽 + for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++) + { + sheet.AutoSizeColumn(i); + } + } + else { - sheet.AutoSizeColumn(i); + // 自动调整列宽 + for (int i = 0; i < mainProperties.Length; i++) + { + sheet.AutoSizeColumn(i); + } } + sheet + + var CellStyle = workbook.CreateCellStyle(); + CellStyle.FillBackgroundColor = IndexedColors.DarkRed.Index; + CellStyle.FillPattern = FillPattern.SolidForeground; + dataRow.GetCell(i).CellStyle = CellStyle; + // 保存Excel文件到MemoryStream workbook.Write(fileStream,true); fileStream.Position = 0; @@ -345,7 +388,7 @@ public class ZbxBase