修改
This commit is contained in:
@@ -8,7 +8,9 @@ using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using MiniExcelLibs;
|
||||
using MiniExcelLibs.OpenXml;
|
||||
using NPOI.HSSF.UserModel;
|
||||
using NPOI.SS.Formula.Functions;
|
||||
using NPOI.SS.UserModel;
|
||||
using NPOI.XSSF.UserModel;
|
||||
using System.Collections;
|
||||
using System.Globalization;
|
||||
@@ -392,33 +394,44 @@ public static class ExcelExportHelper
|
||||
workbook.RemoveSheetAt(1);
|
||||
}
|
||||
|
||||
var memoryStream2 = new MemoryStream();
|
||||
workbook.Write(memoryStream2, true);
|
||||
|
||||
memoryStream2.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
templateStream = memoryStream2;
|
||||
|
||||
}
|
||||
|
||||
// 文件名称 从sheet里面取
|
||||
//fileNmae = workbook.GetSheetName(0);
|
||||
#endregion
|
||||
|
||||
|
||||
ISheet sheet = workbook.GetSheetAt(0); // 获取第一个工作表
|
||||
|
||||
var memoryStream = new MemoryStream();
|
||||
|
||||
|
||||
var config = new OpenXmlConfiguration()
|
||||
for (int i = 0; i < sheet.LastRowNum + 1; i++)
|
||||
{
|
||||
IgnoreTemplateParameterMissing = true,
|
||||
};
|
||||
|
||||
await MiniExcel.SaveAsByTemplateAsync(memoryStream, templateStream.ToArray(), inDto.Data, config);
|
||||
|
||||
memoryStream.Seek(0, SeekOrigin.Begin);
|
||||
IRow row = sheet.GetRow(i);
|
||||
for (int j = 0; j < sheet.LastRowNum; j++)
|
||||
{
|
||||
ICell cell = row.GetCell(j);
|
||||
foreach (var property in inDto.Data.GetType().GetProperties())
|
||||
{
|
||||
var value = property.GetValue(inDto.Data);
|
||||
if (cell!=null&&cell.ToString() == "{{" + property.Name + "}}")
|
||||
{
|
||||
sheet.GetRow(i).GetCell(j).SetCellValue(value.ToString());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return new FileStreamResult(memoryStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
|
||||
var memoryStream2 = new MemoryStream();
|
||||
workbook.Write(memoryStream2, true);
|
||||
|
||||
memoryStream2.Seek(0, SeekOrigin.Begin);
|
||||
|
||||
templateStream = memoryStream2;
|
||||
|
||||
|
||||
return new FileStreamResult(templateStream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
|
||||
{
|
||||
FileDownloadName = $"{(string.IsNullOrEmpty(inDto.ExportFileName) ? "" : inDto.ExportFileName + "_")}{Path.GetFileNameWithoutExtension(fileName)}_{DateTime.Now.ToString("yyyyMMddHHmmss")}.xlsx"
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user