|
@ -11,15 +11,20 @@ using System.Threading.Tasks; |
|
|
using AutoMapper; |
|
|
using AutoMapper; |
|
|
using Faster.Zheng.Winin.AppBase.Filters; |
|
|
using Faster.Zheng.Winin.AppBase.Filters; |
|
|
using Faster.Zheng.Winin.Extensions; |
|
|
using Faster.Zheng.Winin.Extensions; |
|
|
|
|
|
using Faster.Zheng.Winin.Localization; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.AspNetCore.Mvc; |
|
|
using Microsoft.EntityFrameworkCore; |
|
|
using Microsoft.EntityFrameworkCore; |
|
|
|
|
|
using Microsoft.Extensions.Localization; |
|
|
|
|
|
using NPOI.SS.Formula.Functions; |
|
|
using NPOI.SS.UserModel; |
|
|
using NPOI.SS.UserModel; |
|
|
using NPOI.XSSF.UserModel; |
|
|
using NPOI.XSSF.UserModel; |
|
|
using Volo.Abp.Application.Dtos; |
|
|
using Volo.Abp.Application.Dtos; |
|
|
using Volo.Abp.Application.Services; |
|
|
using Volo.Abp.Application.Services; |
|
|
|
|
|
using Volo.Abp.Caching; |
|
|
using Volo.Abp.Domain.Entities; |
|
|
using Volo.Abp.Domain.Entities; |
|
|
using Volo.Abp.Domain.Repositories; |
|
|
using Volo.Abp.Domain.Repositories; |
|
|
using Volo.Abp.EntityFrameworkCore; |
|
|
using Volo.Abp.EntityFrameworkCore; |
|
|
|
|
|
using Volo.Abp.Localization; |
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory; |
|
|
using static Microsoft.EntityFrameworkCore.DbLoggerCategory; |
|
|
using static OpenIddict.Abstractions.OpenIddictConstants; |
|
|
using static OpenIddict.Abstractions.OpenIddictConstants; |
|
|
using static Volo.Abp.UI.Navigation.DefaultMenuNames.Application; |
|
|
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 IRepository<TEntity, TKey> _repository; |
|
|
private readonly IMapper _mapper; |
|
|
private readonly IMapper _mapper; |
|
|
private Func<TCreateInput, Entity> _mapFunc; |
|
|
private Func<TCreateInput, Entity> _mapFunc; |
|
|
|
|
|
protected IStringLocalizer<WininResource> _localizer => LazyServiceProvider.LazyGetRequiredService<IStringLocalizer<WininResource>>(); |
|
|
|
|
|
|
|
|
public ZbxBase(IRepository<TEntity, TKey> repository) : base(repository) |
|
|
public ZbxBase(IRepository<TEntity, TKey> repository) : base(repository) |
|
|
{ |
|
|
{ |
|
@ -263,26 +269,36 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// 导出Excel
|
|
|
/// 【基础】-【导出Excel】【有筛选条件】
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <param name="sfsRequestInputBase"></param>
|
|
|
/// <param name="sfsRequestInputBase">查询条件</param>
|
|
|
/// <param name="filePath"></param>
|
|
|
/// <param name="isRedundance">是否冗余主表数据</param>
|
|
|
|
|
|
/// <param name="isDetailExport">是否导出子表</param>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
[HttpPost("api/[controller]/base/export-to-excel")]
|
|
|
[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 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(); |
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
ISheet sheet = workbook.CreateSheet(typeof(TEntityDto).Name); |
|
|
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); |
|
|
IRow headerRow = sheet.CreateRow(0); |
|
|
for (int i = 0; i < mainProperties.Length; i++) |
|
|
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) |
|
|
if (detailProperties != null) |
|
|
{ |
|
|
{ |
|
|
isHasDetail = true; |
|
|
isHasDetail = true; |
|
|
|
|
|
if (!isDetailExport)//是否要导出子表
|
|
|
|
|
|
{ |
|
|
|
|
|
isHasDetail = false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (isHasDetail) |
|
|
|
|
|
{ |
|
|
|
|
|
headerRow.CreateCell(mainProperties.Length ).SetCellValue("---分割---"); |
|
|
for (int i = 0; i < detailProperties.Length; i++) |
|
|
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) |
|
|
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; |
|
|
var startMainRowIndex = rowIndex; |
|
|
for (int datailCount = 0; datailCount < detailList.Count(); datailCount++) |
|
|
for (int datailCount = 0; datailCount < detailList.Count(); datailCount++) |
|
|
{ |
|
|
{ |
|
|
IRow dataRow = sheet.CreateRow(rowIndex); |
|
|
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++) |
|
|
for (int i = 0; i < mainProperties.Length; i++) |
|
|
{ |
|
|
{ |
|
|
var value = mainProperties[i].GetValue(mainDto); |
|
|
var value = mainProperties[i].GetValue(mainDto); |
|
|
dataRow.CreateCell(i).SetCellValue(value?.ToString()); |
|
|
dataRow.CreateCell(i).SetCellValue(value?.ToString()); |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
rowIndex++; |
|
|
rowIndex++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
var overMainRowIndex = rowIndex; |
|
|
var overMainRowIndex = rowIndex; |
|
|
foreach (var detail in detailList) |
|
|
foreach (var detail in detailList) |
|
|
{ |
|
|
{ |
|
|
if (startMainRowIndex <= overMainRowIndex) |
|
|
if (startMainRowIndex <= overMainRowIndex) |
|
|
{ |
|
|
{ |
|
|
|
|
|
//填充子表数据
|
|
|
var detailRow = sheet.GetRow(startMainRowIndex); |
|
|
var detailRow = sheet.GetRow(startMainRowIndex); |
|
|
for (int i = 0; i < detailProperties.Length; i++) |
|
|
for (int i = 0; i < detailProperties.Length; i++) |
|
|
{ |
|
|
{ |
|
|
var value = detailProperties[i].GetValue(detail); |
|
|
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); |
|
|
var value = mainProperties[i].GetValue(mainDto); |
|
|
dataRow.CreateCell(i).SetCellValue(value?.ToString()); |
|
|
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++; |
|
|
rowIndex++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#region 自动调整列宽
|
|
|
|
|
|
|
|
|
if (isHasDetail) |
|
|
if (isHasDetail) |
|
|
{ |
|
|
{ |
|
|
// 自动调整列宽
|
|
|
// 自动调整列宽
|
|
|
for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++) |
|
|
for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++) |
|
|
{ |
|
|
{ |
|
|
sheet.AutoSizeColumn(i); |
|
|
sheet.AutoSizeColumn(i+splitDetailsColumnNumber); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
else |
|
|
else |
|
@ -374,12 +417,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
var CellStyle = workbook.CreateCellStyle(); |
|
|
|
|
|
CellStyle.FillBackgroundColor = IndexedColors.DarkRed.Index; |
|
|
|
|
|
CellStyle.FillPattern = FillPattern.SolidForeground; |
|
|
|
|
|
dataRow.GetCell(i).CellStyle = CellStyle; |
|
|
|
|
|
|
|
|
|
|
|
// 保存Excel文件到MemoryStream
|
|
|
// 保存Excel文件到MemoryStream
|
|
|
workbook.Write(fileStream,true); |
|
|
workbook.Write(fileStream,true); |
|
@ -395,6 +433,15 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
return fileContentResult; |
|
|
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
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
#region 私有处理
|
|
|
#region 私有处理
|
|
|