|
@@ -1,4 +1,5 @@
|
|
|
|
|
|
+using DocumentFormat.OpenXml.ExtendedProperties;
|
|
|
using Hotline.Application.Tools;
|
|
|
using Hotline.Share.Dtos.CallCenter;
|
|
|
using Hotline.Share.Dtos.Order;
|
|
@@ -12,6 +13,7 @@ using Microsoft.AspNetCore.Mvc;
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
using MiniExcelLibs;
|
|
|
using NPOI.HPSF;
|
|
|
+using NPOI.SS.Formula.Functions;
|
|
|
using SqlSugar;
|
|
|
using System.Net.Http;
|
|
|
using System.Reflection;
|
|
@@ -69,6 +71,41 @@ namespace Hotline.Application.ExportExcel
|
|
|
return GetExcelStream(dto, items, func).GetExcelFile(fileName);
|
|
|
}
|
|
|
|
|
|
+ public FileStreamResult GetExcelFile(Type typeT, Type typeD, object dto, IList<object> items, string fileName, string? totalFiledName)
|
|
|
+ {
|
|
|
+ var fieldsAll = typeT.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
|
|
|
+ var fields = fieldsAll.Where(f => IsNumericType(f.FieldType)); // 只选择数值类型字段
|
|
|
+ var totalField = fieldsAll.Where(m => m.Name.Contains(totalFiledName)).First();
|
|
|
+
|
|
|
+ var sumDict = new Dictionary<FieldInfo, dynamic>();
|
|
|
+
|
|
|
+ foreach (var field in fields)
|
|
|
+ {
|
|
|
+ sumDict[field] = 0;
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (var item in items)
|
|
|
+ {
|
|
|
+ foreach (var field in fields)
|
|
|
+ {
|
|
|
+ var value = field.GetValue(item);
|
|
|
+ if (value != null)
|
|
|
+ {
|
|
|
+ sumDict[field] += (dynamic)value; // 使用 dynamic 累加
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ var resultItem = Activator.CreateInstance(typeT);
|
|
|
+ totalField.SetValue(resultItem, "合计");
|
|
|
+ foreach (var field in fields)
|
|
|
+ {
|
|
|
+ field.SetValue(resultItem, sumDict[field]);
|
|
|
+ }
|
|
|
+ items.Add(resultItem);
|
|
|
+ return GetExcelFile(typeT, typeD, dto, items, fileName);
|
|
|
+ }
|
|
|
+
|
|
|
public FileStreamResult GetExcelFile(Type typeT, Type typeD, object dto, IList<object> items, string fileName)
|
|
|
{
|
|
|
var columnInfos = typeD.GetProperty("ColumnInfos")?.GetValue(dto) as List<ColumnInfo>;
|
|
@@ -142,7 +179,7 @@ namespace Hotline.Application.ExportExcel
|
|
|
return type == typeof(int) || type == typeof(float) || type == typeof(double) ||
|
|
|
type == typeof(decimal) || type == typeof(long) || type == typeof(short) ||
|
|
|
type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) ||
|
|
|
- type == typeof(ushort) || type == typeof(sbyte);
|
|
|
+ type == typeof(ushort) || type == typeof(sbyte) || type == typeof(double?);
|
|
|
}
|
|
|
}
|
|
|
}
|