Browse Source

修改导出

master
郑勃旭 2 years ago
parent
commit
6f6720ef3a
  1. 6
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs
  2. 95
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs
  3. 6
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/WininApplicationModule.cs
  4. 4
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/WininWebModule.cs

6
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application.Contracts/AppBase/IZbxBase.cs

@ -13,10 +13,4 @@ public interface IZbxBase<TEntity, TEntityDto, TPagedAndSortedResultRequestDto,
Task<PagedResultDto<TEntityDto>> GetPageListByFilterAsync(SfsRequestInputBase sfsRequestInputBase,
bool includeDetails = false, CancellationToken cancellationToken = default);
/// <summary>
/// 导出Excel
/// </summary>
/// <param name="sfsRequestInputBase"></param>
/// <param name="filePath"></param>
Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase);
}

95
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<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
private readonly IRepository<TEntity, TKey> _repository;
private readonly IMapper _mapper;
private Func<TCreateInput, Entity> _mapFunc;
protected IStringLocalizer<WininResource> _localizer => LazyServiceProvider.LazyGetRequiredService<IStringLocalizer<WininResource>>();
public ZbxBase(IRepository<TEntity, TKey> repository) : base(repository)
{
@ -263,26 +269,36 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
/// <summary>
/// 导出Excel
/// 【基础】-【导出Excel】【有筛选条件】
/// </summary>
/// <param name="sfsRequestInputBase"></param>
/// <param name="filePath"></param>
/// <param name="sfsRequestInputBase">查询条件</param>
/// <param name="isRedundance">是否冗余主表数据</param>
/// <param name="isDetailExport">是否导出子表</param>
/// <returns></returns>
[HttpPost("api/[controller]/base/export-to-excel")]
public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase)
public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase,bool isRedundance,bool isDetailExport = true)
{
var isHasDetail = false;//是否包含从表
var data = (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items;
var fileStream = new MemoryStream();
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<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (detailProperties != null)
{
isHasDetail = true;
if (!isDetailExport)//是否要导出子表
{
isHasDetail = false;
}
if (isHasDetail)
{
headerRow.CreateCell(mainProperties.Length ).SetCellValue("---分割---");
for (int i = 0; i < detailProperties.Length; i++)
{
headerRow.CreateCell(mainProperties.Length + i).SetCellValue(detailProperties[i].Name);
headerRow.CreateCell(mainProperties.Length + splitDetailsColumnNumber + i)
.SetCellValue(detailProperties[i].Name);
var headCell = headerRow.GetCell(mainProperties.Length + splitDetailsColumnNumber + i);
headCell.CellStyle = excelDetailsColor;
}
}
}
@ -303,33 +331,52 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
if (isHasDetail)
{
// 获取从表数据
var detailsIndex = mainProperties.FindIndex(p => p.Name == "Details");
var detailsIndex = mainAllProperties.FindIndex(p => p.Name == "Details");
//从表
var detailList = (IEnumerable<object>)mainProperties[detailsIndex].GetValue(mainDto);
var detailList = (IEnumerable<object>)mainAllProperties[detailsIndex].GetValue(mainDto);
var startMainRowIndex = rowIndex;
for (int datailCount = 0; datailCount < detailList.Count(); datailCount++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
if (isRedundance)
{
// 填充主表数据
for (int i = 0; i < mainProperties.Length; i++)
{
var value = mainProperties[i].GetValue(mainDto);
dataRow.CreateCell(i).SetCellValue(value?.ToString());
}
}
else
{
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<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
{
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;
}
}
@ -357,12 +398,14 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
rowIndex++;
}
#region 自动调整列宽
if (isHasDetail)
{
// 自动调整列宽
for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++)
{
sheet.AutoSizeColumn(i);
sheet.AutoSizeColumn(i+splitDetailsColumnNumber);
}
}
else
@ -374,12 +417,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
}
}
var CellStyle = workbook.CreateCellStyle();
CellStyle.FillBackgroundColor = IndexedColors.DarkRed.Index;
CellStyle.FillPattern = FillPattern.SolidForeground;
dataRow.GetCell(i).CellStyle = CellStyle;
#endregion
// 保存Excel文件到MemoryStream
workbook.Write(fileStream,true);
@ -395,6 +433,15 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
return fileContentResult;
}
private static ICellStyle SetExcelDetailsColor(IWorkbook workbook)
{
var cellStyle = workbook.CreateCellStyle();
cellStyle.FillBackgroundColor = IndexedColors.LightOrange.Index;
cellStyle.FillForegroundColor = IndexedColors.LightOrange.Index;
cellStyle.FillPattern = FillPattern.SolidForeground;
return cellStyle;
}
#endregion
#region 私有处理

6
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/WininApplicationModule.cs

@ -1,11 +1,15 @@
using Volo.Abp.Account;
using Faster.Zheng.Winin.AppBusiness.TestSchool;
using Faster.Zheng.Winin.Localization;
using Volo.Abp.Account;
using Volo.Abp.AutoMapper;
using Volo.Abp.FeatureManagement;
using Volo.Abp.Identity;
using Volo.Abp.Localization;
using Volo.Abp.Modularity;
using Volo.Abp.PermissionManagement;
using Volo.Abp.SettingManagement;
using Volo.Abp.TenantManagement;
using Volo.Abp.VirtualFileSystem;
namespace Faster.Zheng.Winin;

4
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/WininWebModule.cs

@ -44,6 +44,7 @@ using Volo.Abp.AspNetCore.ExceptionHandling;
using Volo.Abp.Settings;
using Polly;
using Autofac.Core;
using Faster.Zheng.Winin.AppBusiness.TestSchool;
using static Faster.Zheng.Winin.Web.WininWebAutoMapperProfile;
namespace Faster.Zheng.Winin.Web;
@ -282,7 +283,8 @@ public class WininWebModule : AbpModule
/// </summary>
private void ConfigureLanguage(IApplicationBuilder applicationBuilder)
{
applicationBuilder.ApplicationServices.GetService<ISettingDefinitionManager>()!.Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans";
applicationBuilder.ApplicationServices.GetService<ISettingDefinitionManager>()!
.Get(LocalizationSettingNames.DefaultLanguage).DefaultValue = "zh-Hans";
}
/// <summary>

Loading…
Cancel
Save