安虹睿 1 year ago
parent
commit
a59e8c2865
  1. 83
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs

83
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs

@ -285,7 +285,10 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
IWorkbook workbook = new XSSFWorkbook(); IWorkbook workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet(typeof(TEntityDto).Name); var sheet = workbook.CreateSheet(typeof(TEntityDto).Name);
var splitDetailsColumnNumber = 1; //分割主表和从表的列数量 var splitDetailsColumnNumber = 1; //分割主表和从表的列数量
var excelDetailsColor = SetExcelDetailsColor(workbook); //字表样式 var excelDetailsCellStyle = SetExcelDetailsCellStyle(workbook); //子表单元格样式
var excelSplitCellStyle = SetSplitCellStyle(workbook); //分割单元格样式
var excelOnlyMainCellStyle = SetExcelOnlyMainCellStyle(workbook);
var excelHeadCellStyle = SetExcelHeadCellStyle(workbook);
// 获取主表的属性 创建主表 表头 // 获取主表的属性 创建主表 表头
var mainAllProperties = typeof(TEntityDto).GetProperties(); var mainAllProperties = typeof(TEntityDto).GetProperties();
@ -305,13 +308,15 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion #endregion
var headerRow = sheet.CreateRow(0); var headerRow = sheet.CreateRow(0);//标头列
for (var i = 0; i < mainProperties.Length; i++) for (var i = 0; i < mainProperties.Length; i++)
{ {
var englishName = mainProperties[i].Name; var englishName = mainProperties[i].Name;
//本地化 //本地化
var localizerName = _localizer[typeof(TEntity).Name + englishName]; var localizerName = _localizer[typeof(TEntity).Name + englishName];
headerRow.CreateCell(i).SetCellValue(localizerName); var headCell = headerRow.CreateCell(i);
headCell.SetCellValue(localizerName);
headCell.CellStyle = excelHeadCellStyle;
} }
// 获取从表的属性 创建从表 表头 // 获取从表的属性 创建从表 表头
@ -332,7 +337,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (isHasDetail) if (isHasDetail)
{ {
headerRow.CreateCell(mainProperties.Length).SetCellValue("---分割---"); headerRow.CreateCell(mainProperties.Length).SetCellValue("---分割---");
#region 用户个性导出 从表 #region 用户个性导出 从表
@ -355,7 +360,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i) headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(detailProperties[i].Name); .SetCellValue(detailProperties[i].Name);
var headCell = headerRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i); var headCell = headerRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
headCell.CellStyle = excelDetailsColor; headCell.CellStyle = excelHeadCellStyle;
} }
} }
} }
@ -368,7 +373,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{ {
// 获取从表数据 // 获取从表数据
var detailsIndex = mainAllProperties.FindIndex(p => p.Name == "Details"); var detailsIndex = mainAllProperties.FindIndex(p => p.Name == "Details");
// //
var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto); var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto);
var startMainRowIndex = rowIndex; var startMainRowIndex = rowIndex;
for (var datailCount = 0; datailCount < detailList.Count(); datailCount++) for (var datailCount = 0; datailCount < detailList.Count(); datailCount++)
@ -407,13 +412,17 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{ {
//填充子表数据 //填充子表数据
var detailRow = sheet.GetRow(startMainRowIndex); var detailRow = sheet.GetRow(startMainRowIndex);
var splitCell = detailRow.CreateCell(mainProperties.Length);
splitCell.CellStyle = excelSplitCellStyle;
for (var i = 0; i < detailProperties.Length; i++) for (var i = 0; i < detailProperties.Length; i++)
{ {
var value = detailProperties[i].GetValue(detail); var value = detailProperties[i].GetValue(detail);
detailRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i) detailRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(value?.ToString()); .SetCellValue(value?.ToString());
var detailCell = detailRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i); var detailCell = detailRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
detailCell.CellStyle = excelDetailsColor; detailCell.CellStyle = excelDetailsCellStyle;
} }
} }
@ -429,6 +438,10 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
var value = mainProperties[i].GetValue(mainDto); var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString()); dataRow.CreateCell(i).SetCellValue(value?.ToString());
} }
if (rowIndex % 2 == 0)
{
dataRow.RowStyle = excelOnlyMainCellStyle;
}
} }
//添加1个空行将2条数据分割开 //添加1个空行将2条数据分割开
@ -440,9 +453,11 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (isHasDetail) if (isHasDetail)
{ {
// 自动调整列宽 // 自动调整列宽
for (var i = 0; i < mainProperties.Length + detailProperties.Length; i++) for (var i = 0; i < mainProperties.Length + splitDetailsColumnNumber + detailProperties.Length; i++)
{ {
sheet.AutoSizeColumn(i + splitDetailsColumnNumber); sheet.AutoSizeColumn(i);
sheet.SetColumnWidth(i, Math.Max(sheet.GetColumnWidth(i) + 150, 265 * 15));
sheet.SetColumnWidth(mainProperties.Length, 3600);
} }
} }
else else
@ -451,6 +466,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
for (var i = 0; i < mainProperties.Length; i++) for (var i = 0; i < mainProperties.Length; i++)
{ {
sheet.AutoSizeColumn(i); sheet.AutoSizeColumn(i);
sheet.SetColumnWidth(i, Math.Max(sheet.GetColumnWidth(i) + 150, 265 * 15));
} }
} }
@ -490,6 +506,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
bool includeDetails = false, CancellationToken cancellationToken = default) bool includeDetails = false, CancellationToken cancellationToken = default)
{ {
var query = await Repository.WithDetailsAsync(); var query = await Repository.WithDetailsAsync();
//var query = await Repository.GetQueryableAsync();
var entities = query.Where(expression); var entities = query.Where(expression);
entities = GetSortingQueryable(entities, sorting); entities = GetSortingQueryable(entities, sorting);
@ -548,7 +565,53 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
/// </summary> /// </summary>
/// <param name="workbook"></param> /// <param name="workbook"></param>
/// <returns></returns> /// <returns></returns>
private static ICellStyle SetExcelDetailsColor(IWorkbook workbook) private static ICellStyle SetExcelDetailsCellStyle(IWorkbook workbook)
{
var cellStyle = workbook.CreateCellStyle();
cellStyle.FillBackgroundColor = IndexedColors.Grey25Percent.Index;
cellStyle.FillForegroundColor = IndexedColors.Grey25Percent.Index;
cellStyle.FillPattern = FillPattern.SolidForeground;
return cellStyle;
}
/// <summary>
/// 导出设置只有主表时的交替行 单元格样式
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>
private static ICellStyle SetExcelOnlyMainCellStyle(IWorkbook workbook)
{
var cellStyle = workbook.CreateCellStyle();
cellStyle.FillBackgroundColor = IndexedColors.Grey25Percent.Index;
cellStyle.FillForegroundColor = IndexedColors.Grey25Percent.Index;
cellStyle.FillPattern = FillPattern.SolidForeground;
return cellStyle;
}
/// <summary>
/// 设置分割单元格的演示
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>
private static ICellStyle SetSplitCellStyle(IWorkbook workbook)
{
var cellStyle = workbook.CreateCellStyle();
cellStyle.BorderLeft = BorderStyle.MediumDashed;
cellStyle.BorderRight = BorderStyle.MediumDashed;
cellStyle.LeftBorderColor = IndexedColors.BrightGreen.Index;
cellStyle.RightBorderColor = IndexedColors.Grey25Percent.Index;
cellStyle.FillBackgroundColor = IndexedColors.White.Index;
cellStyle.FillForegroundColor = IndexedColors.White.Index;
cellStyle.FillPattern = FillPattern.ThickVerticalBands;
return cellStyle;
}
/// <summary>
/// 导出设置表头单元格样式
/// </summary>
/// <param name="workbook"></param>
/// <returns></returns>
private static ICellStyle SetExcelHeadCellStyle(IWorkbook workbook)
{ {
var cellStyle = workbook.CreateCellStyle(); var cellStyle = workbook.CreateCellStyle();
cellStyle.FillBackgroundColor = IndexedColors.LightOrange.Index; cellStyle.FillBackgroundColor = IndexedColors.LightOrange.Index;

Loading…
Cancel
Save