Browse Source

修改导出

master
郑渤旭[Irelia] 1 year ago
parent
commit
2d7941c2f9
  1. 89
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs

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

@ -33,8 +33,6 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
where TEntityDto : IEntityDto<TKey>
{
private readonly IRepository<TEntity, TKey> _repository;
private readonly IMapper _mapper;
private Func<TCreateInput, Entity> _mapFunc;
protected IStringLocalizer<WininResource> _localizer =>
LazyServiceProvider.LazyGetRequiredService<IStringLocalizer<WininResource>>();
@ -42,7 +40,6 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
protected ExportCustomUserSettingAppService _exportCustomUserSettingAppService =>
LazyServiceProvider.LazyGetRequiredService<ExportCustomUserSettingAppService>();
public ZbxBase(IRepository<TEntity, TKey> repository) : base(repository)
{
_repository = repository;
@ -61,6 +58,8 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
public async Task<PagedResultDto<TEntityDto>> GetPageListByFilterAsync(SfsRequestInputBase sfsRequestInputBase,
bool includeDetails = false, CancellationToken cancellationToken = default)
{
await CheckGetListPolicyAsync();
var expression = sfsRequestInputBase.Condition.Filters?.Count > 0
? sfsRequestInputBase.Condition.Filters.ToLambda<TEntity>()
: p => true;
@ -286,7 +285,10 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
IWorkbook workbook = new XSSFWorkbook();
var sheet = workbook.CreateSheet(typeof(TEntityDto).Name);
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();
@ -306,13 +308,15 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
#endregion
var headerRow = sheet.CreateRow(0);
var headerRow = sheet.CreateRow(0);//标头列
for (var i = 0; i < mainProperties.Length; i++)
{
var englishName = mainProperties[i].Name;
//本地化
var localizerName = _localizer[typeof(TEntity).Name + englishName];
headerRow.CreateCell(i).SetCellValue(localizerName);
var headCell = headerRow.CreateCell(i);
headCell.SetCellValue(localizerName);
headCell.CellStyle = excelHeadCellStyle;
}
// 获取从表的属性 创建从表 表头
@ -333,7 +337,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (isHasDetail)
{
headerRow.CreateCell(mainProperties.Length).SetCellValue("---分割---");
headerRow.CreateCell(mainProperties.Length).SetCellValue("---分割---");
#region 用户个性导出 从表
@ -356,7 +360,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(detailProperties[i].Name);
var headCell = headerRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
headCell.CellStyle = excelDetailsColor;
headCell.CellStyle = excelHeadCellStyle;
}
}
}
@ -369,7 +373,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{
// 获取从表数据
var detailsIndex = mainAllProperties.FindIndex(p => p.Name == "Details");
//
//
var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto);
var startMainRowIndex = rowIndex;
for (var datailCount = 0; datailCount < detailList.Count(); datailCount++)
@ -408,13 +412,17 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{
//填充子表数据
var detailRow = sheet.GetRow(startMainRowIndex);
var splitCell = detailRow.CreateCell(mainProperties.Length);
splitCell.CellStyle = excelSplitCellStyle;
for (var i = 0; i < detailProperties.Length; i++)
{
var value = detailProperties[i].GetValue(detail);
detailRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(value?.ToString());
var detailCell = detailRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
detailCell.CellStyle = excelDetailsColor;
detailCell.CellStyle = excelDetailsCellStyle;
}
}
@ -430,6 +438,10 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString());
}
if (rowIndex % 2 == 0)
{
dataRow.RowStyle = excelOnlyMainCellStyle;
}
}
//添加1个空行将2条数据分割开
@ -441,9 +453,11 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
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
@ -452,6 +466,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
for (var i = 0; i < mainProperties.Length; i++)
{
sheet.AutoSizeColumn(i);
sheet.SetColumnWidth(i, Math.Max(sheet.GetColumnWidth(i) + 150, 265 * 15));
}
}
@ -491,6 +506,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
bool includeDetails = false, CancellationToken cancellationToken = default)
{
var query = await Repository.WithDetailsAsync();
//var query = await Repository.GetQueryableAsync();
var entities = query.Where(expression);
entities = GetSortingQueryable(entities, sorting);
@ -549,7 +565,53 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
/// </summary>
/// <param name="workbook"></param>
/// <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();
cellStyle.FillBackgroundColor = IndexedColors.LightOrange.Index;
@ -559,4 +621,5 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
#endregion
}
}
Loading…
Cancel
Save