diff --git a/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs b/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs index 019678b..0d64ae3 100644 --- a/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs +++ b/Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs @@ -13,10 +13,4 @@ public interface IZbxBase> GetPageListByFilterAsync(SfsRequestInputBase sfsRequestInputBase, bool includeDetails = false, CancellationToken cancellationToken = default); - /// - /// 导出Excel - /// - /// - /// - Task ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase); } \ No newline at end of file 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 7a51922..2093427 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 @@ -11,15 +11,20 @@ using System.Threading.Tasks; using AutoMapper; using Faster.Zheng.Winin.AppBase.Filters; using Faster.Zheng.Winin.Extensions; +using Faster.Zheng.Winin.Localization; using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Localization; +using NPOI.SS.Formula.Functions; using NPOI.SS.UserModel; using NPOI.XSSF.UserModel; using Volo.Abp.Application.Dtos; using Volo.Abp.Application.Services; +using Volo.Abp.Caching; using Volo.Abp.Domain.Entities; using Volo.Abp.Domain.Repositories; using Volo.Abp.EntityFrameworkCore; +using Volo.Abp.Localization; using static Microsoft.EntityFrameworkCore.DbLoggerCategory; using static OpenIddict.Abstractions.OpenIddictConstants; using static Volo.Abp.UI.Navigation.DefaultMenuNames.Application; @@ -36,6 +41,7 @@ public class ZbxBase _repository; private readonly IMapper _mapper; private Func _mapFunc; + protected IStringLocalizer _localizer => LazyServiceProvider.LazyGetRequiredService>(); public ZbxBase(IRepository repository) : base(repository) { @@ -263,26 +269,36 @@ public class ZbxBase - /// 导出Excel + /// 【基础】-【导出Excel】【有筛选条件】 /// - /// - /// + /// 查询条件 + /// 是否冗余主表数据 + /// 是否导出子表 + /// [HttpPost("api/[controller]/base/export-to-excel")] - public virtual async Task ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase) + public virtual async Task ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase,bool isRedundance,bool isDetailExport = true) { var isHasDetail = false;//是否包含从表 - var data= (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items; - var fileStream = new MemoryStream(); + var data = (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items; + + var fileStream = new MemoryStream();//文件流 IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet(typeof(TEntityDto).Name); + int splitDetailsColumnNumber = 1;//分割主表和从表的列数量 + var excelDetailsColor = SetExcelDetailsColor(workbook);//字表样式 // 获取主表的属性 创建主表 表头 - var mainProperties = typeof(TEntityDto).GetProperties(); + var mainAllProperties = typeof(TEntityDto).GetProperties(); + var mainProperties= mainAllProperties.Where(p=>p.Name.ToLower()!="details").ToArray();//去除details属性否则导出时会带出来 + IRow headerRow = sheet.CreateRow(0); for (int i = 0; i < mainProperties.Length; i++) { - headerRow.CreateCell(i).SetCellValue(mainProperties[i].Name); + var englishName=mainProperties[i].Name; + //本地化 + //var localizerName = _localizer[englishName]; + headerRow.CreateCell(i).SetCellValue(englishName); } // 获取从表的属性 创建从表 表头 @@ -290,9 +306,21 @@ public class ZbxBase p.Name == "Details"); + var detailsIndex = mainAllProperties.FindIndex(p => p.Name == "Details"); //从表 - var detailList = (IEnumerable)mainProperties[detailsIndex].GetValue(mainDto); + var detailList = (IEnumerable)mainAllProperties[detailsIndex].GetValue(mainDto); var startMainRowIndex = rowIndex; for (int datailCount = 0; datailCount < detailList.Count(); datailCount++) { IRow dataRow = sheet.CreateRow(rowIndex); - // 填充主表数据 - for (int i = 0; i < mainProperties.Length; i++) + if (isRedundance) + { + // 填充主表数据 + for (int i = 0; i < mainProperties.Length; i++) + { + var value = mainProperties[i].GetValue(mainDto); + dataRow.CreateCell(i).SetCellValue(value?.ToString()); + } + } + else { - var value = mainProperties[i].GetValue(mainDto); - dataRow.CreateCell(i).SetCellValue(value?.ToString()); + if (datailCount == 0) + { + // 填充主表数据 + for (int i = 0; i < mainProperties.Length; i++) + { + var value = mainProperties[i].GetValue(mainDto); + dataRow.CreateCell(i).SetCellValue(value?.ToString()); + } + } } rowIndex++; } + var overMainRowIndex = rowIndex; foreach (var detail in detailList) - { + { 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()); + detailRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i).SetCellValue(value?.ToString()); + var detailCell = detailRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i); + detailCell.CellStyle = excelDetailsColor; } } @@ -344,12 +391,6 @@ public class ZbxBase private void ConfigureLanguage(IApplicationBuilder applicationBuilder) { - applicationBuilder.ApplicationServices.GetService()!.Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans"; + applicationBuilder.ApplicationServices.GetService()! + .Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans"; } ///