|
@ -264,44 +264,92 @@ public class ZbxBase<TEntity, TEntityDto, TKey, TPagedAndSortedResultRequestDto, |
|
|
/// <summary>
|
|
|
/// <summary>
|
|
|
/// 导出Excel
|
|
|
/// 导出Excel
|
|
|
/// </summary>
|
|
|
/// </summary>
|
|
|
/// <typeparam name="T"></typeparam>
|
|
|
/// <param name="sfsRequestInputBase"></param>
|
|
|
/// <param name="data"></param>
|
|
|
|
|
|
/// <param name="filePath"></param>
|
|
|
/// <param name="filePath"></param>
|
|
|
[HttpPost("api/[controller]/base/export-to-excel")]
|
|
|
[HttpPost("api/[controller]/base/export-to-excel")]
|
|
|
public void ExportToExcel<T>(IEnumerable<T> data, string filePath) |
|
|
public virtual async Task<IActionResult> ExportToExcelAsync(SfsRequestInputBase sfsRequestInputBase) |
|
|
{ |
|
|
{ |
|
|
|
|
|
var data= (await GetPageListByFilterAsync(sfsRequestInputBase,true)).Items; |
|
|
|
|
|
|
|
|
|
|
|
var fileStream = new MemoryStream(); |
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
IWorkbook workbook = new XSSFWorkbook(); |
|
|
ISheet sheet = workbook.CreateSheet("Sheet1"); |
|
|
ISheet sheet = workbook.CreateSheet("Sheet1"); |
|
|
|
|
|
|
|
|
// 获取泛型参数的类型
|
|
|
// 获取主表和从表的属性
|
|
|
var type = typeof(T); |
|
|
var mainProperties = typeof(TEntityDto).GetProperties(); |
|
|
var properties = type.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 < properties.Length; i++) |
|
|
for (int i = 0; i < mainProperties.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
headerRow.CreateCell(i).SetCellValue(mainProperties[i].Name); |
|
|
|
|
|
} |
|
|
|
|
|
for (int i = 0; i < detailProperties.Length; i++) |
|
|
{ |
|
|
{ |
|
|
headerRow.CreateCell(i).SetCellValue(properties[i].Name); |
|
|
headerRow.CreateCell(mainProperties.Length + i).SetCellValue(detailProperties[i].Name); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 填充数据行
|
|
|
// 填充数据行
|
|
|
int rowIndex = 1; |
|
|
int rowIndex = 1; |
|
|
foreach (var item in data) |
|
|
foreach (var mainDto in data) |
|
|
{ |
|
|
{ |
|
|
IRow dataRow = sheet.CreateRow(rowIndex); |
|
|
IRow dataRow = sheet.CreateRow(rowIndex); |
|
|
for (int i = 0; i < properties.Length; i++) |
|
|
|
|
|
|
|
|
// 填充主表数据
|
|
|
|
|
|
for (int i = 0; i < mainProperties.Length; i++) |
|
|
{ |
|
|
{ |
|
|
var value = properties[i].GetValue(item); |
|
|
var value = mainProperties[i].GetValue(mainDto); |
|
|
dataRow.CreateCell(i).SetCellValue(value?.ToString()); |
|
|
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); |
|
|
|
|
|
} |
|
|
|
|
|
else |
|
|
|
|
|
{ |
|
|
|
|
|
// 获取已创建的行用于显示从表数据
|
|
|
|
|
|
detailRow = sheet.GetRow(detailCellIndex); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < detailProperties.Length; i++) |
|
|
|
|
|
{ |
|
|
|
|
|
var value = detailProperties[i].GetValue(detailItem); |
|
|
|
|
|
detailRow.CreateCell(mainProperties.Length + i).SetCellValue(value?.ToString()); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
detailCellIndex++; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
rowIndex++; |
|
|
rowIndex++; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
// 保存Excel文件
|
|
|
// 自动调整列宽
|
|
|
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) |
|
|
for (int i = 0; i < mainProperties.Length + detailProperties.Length; i++) |
|
|
{ |
|
|
{ |
|
|
workbook.Write(fileStream,true); |
|
|
sheet.AutoSizeColumn(i); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 保存Excel文件到MemoryStream
|
|
|
|
|
|
workbook.Write(fileStream,true); |
|
|
|
|
|
fileStream.Position = 0; |
|
|
|
|
|
|
|
|
|
|
|
// 创建FileContentResult返回Excel文件
|
|
|
|
|
|
var fileContentResult = new FileContentResult(fileStream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") |
|
|
|
|
|
{ |
|
|
|
|
|
FileDownloadName = "export.xlsx" |
|
|
|
|
|
}; |
|
|
|
|
|
await Task.CompletedTask; |
|
|
|
|
|
|
|
|
|
|
|
return fileContentResult; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
#endregion
|
|
|
#endregion
|
|
|