boxu.zheng 1 year ago
parent
commit
81a5c33350
  1. 89
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs
  2. 4
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/Properties/launchSettings.json
  3. 2
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/appsettings.json

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

@ -1,6 +1,7 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Linq.Dynamic.Core;
@ -269,30 +270,44 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
[HttpPost("api/[controller]/base/export-to-excel")]
public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase)
{
var isHasDetail = false;//是否包含从表
var data= (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items;
var fileStream = new MemoryStream();
IWorkbook workbook = new XSSFWorkbook();
ISheet sheet = workbook.CreateSheet("Sheet1");
ISheet sheet = workbook.CreateSheet(typeof(TEntityDto).Name);
// 获取主表和从表的属性
// 获取主表的属性 创建主表 表头
var mainProperties = typeof(TEntityDto).GetProperties();
var detailProperties = typeof(TEntityDto).GetProperty("Details").PropertyType.GetGenericArguments()[0].GetProperties();
// 创建表头
IRow headerRow = sheet.CreateRow(0);
for (int i = 0; i < mainProperties.Length; i++)
{
headerRow.CreateCell(i).SetCellValue(mainProperties[i].Name);
}
// 获取从表的属性 创建从表 表头
var detailProperties = typeof(TEntityDto).GetProperty("Details")?.PropertyType.GetGenericArguments()[0].GetProperties();
if (detailProperties != null)
{
isHasDetail = true;
for (int i = 0; i < detailProperties.Length; i++)
{
headerRow.CreateCell(mainProperties.Length + i).SetCellValue(detailProperties[i].Name);
}
}
// 填充数据行
int rowIndex = 1;
foreach (var mainDto in data)
{
if (isHasDetail)
{
// 获取从表数据
var detailsIndex = mainProperties.FindIndex(p => p.Name == "Details");
//从表
var detailList = (IEnumerable<object>)mainProperties[detailsIndex].GetValue(mainDto);
var startMainRowIndex = rowIndex;
for (int datailCount = 0; datailCount < detailList.Count(); datailCount++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
@ -303,40 +318,68 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
dataRow.CreateCell(i).SetCellValue(value?.ToString());
}
// 填充从表数据
var detailList = (IEnumerable<object>)mainProperties[0].GetValue(mainDto);
int detailCellIndex = mainProperties.Length;
foreach (var detailItem in detailList)
{
IRow detailRow;
if (rowIndex == 1)
{
// 创建新的行用于显示从表数据
detailRow = sheet.CreateRow(detailCellIndex);
rowIndex++;
}
else
var overMainRowIndex = rowIndex;
foreach (var detail in detailList)
{
// 获取已创建的行用于显示从表数据
detailRow = sheet.GetRow(detailCellIndex);
}
if (startMainRowIndex <= overMainRowIndex)
{
var detailRow = sheet.GetRow(startMainRowIndex);
for (int i = 0; i < detailProperties.Length; i++)
{
var value = detailProperties[i].GetValue(detailItem);
var value = detailProperties[i].GetValue(detail);
detailRow.CreateCell(mainProperties.Length + i).SetCellValue(value?.ToString());
}
}
detailCellIndex++;
startMainRowIndex++;
}
}
else
{
IRow dataRow = sheet.CreateRow(rowIndex);
// 填充主表数据
for (int i = 0; i < mainProperties.Length; i++)
{
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;
}
}
//添加1个空行将2条数据分割开
rowIndex++;
}
if (isHasDetail)
{
// 自动调整列宽
for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++)
{
sheet.AutoSizeColumn(i);
}
}
else
{
// 自动调整列宽
for (int i = 0; i < mainProperties.Length; i++)
{
sheet.AutoSizeColumn(i);
}
}
sheet
var CellStyle = workbook.CreateCellStyle();
CellStyle.FillBackgroundColor = IndexedColors.DarkRed.Index;
CellStyle.FillPattern = FillPattern.SolidForeground;
dataRow.GetCell(i).CellStyle = CellStyle;
// 保存Excel文件到MemoryStream
workbook.Write(fileStream,true);
@ -345,7 +388,7 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto,
// 创建FileContentResult返回Excel文件
var fileContentResult = new FileContentResult(fileStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = "export.xlsx"
FileDownloadName = typeof(TEntityDto).Name+".xlsx"
};
await Task.CompletedTask;

4
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/Properties/launchSettings.json

@ -3,7 +3,7 @@
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "https://localhost:44392/",
"applicationUrl": "https://localhost:60069/",
"sslPort": 44392
}
},
@ -21,7 +21,7 @@
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"applicationUrl": "https://localhost:44392/"
"applicationUrl": "https://localhost:60069/"
}
}
}

2
Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Web/appsettings.json

@ -1,6 +1,6 @@
{
"App": {
"SelfUrl": "https://localhost:44392",
"SelfUrl": "https://localhost:60069",
//
"CorsOrigins": [
"http://localhost:9527",

Loading…
Cancel
Save