boxu.zheng 1 year ago
parent
commit
81a5c33350
  1. 117
      Code/Be/Faster.Zheng.Winin/src/Faster.Zheng.Winin.Application/AppBase/ZbxBase.cs
  2. 14
      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

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

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

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

@ -3,7 +3,7 @@
"windowsAuthentication": false, "windowsAuthentication": false,
"anonymousAuthentication": true, "anonymousAuthentication": true,
"iisExpress": { "iisExpress": {
"applicationUrl": "https://localhost:44392/", "applicationUrl": "https://localhost:60069/",
"sslPort": 44392 "sslPort": 44392
} }
}, },
@ -16,12 +16,12 @@
} }
}, },
"Faster.Zheng.Winin.Web": { "Faster.Zheng.Winin.Web": {
"commandName": "Project", "commandName": "Project",
"launchBrowser": true, "launchBrowser": true,
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "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": { "App": {
"SelfUrl": "https://localhost:44392", "SelfUrl": "https://localhost:60069",
// //
"CorsOrigins": [ "CorsOrigins": [
"http://localhost:9527", "http://localhost:9527",

Loading…
Cancel
Save