ExportApplication.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 
  2. using Hotline.Application.Tools;
  3. using Hotline.Share.Dtos.CallCenter;
  4. using Hotline.Share.Dtos.Order;
  5. using Hotline.Share.Enums.Article;
  6. using Hotline.Share.Tools;
  7. using Hotline.Tools;
  8. using Mapster;
  9. using MapsterMapper;
  10. using Microsoft.AspNetCore.Http;
  11. using Microsoft.AspNetCore.Mvc;
  12. using Microsoft.Extensions.DependencyInjection;
  13. using MiniExcelLibs;
  14. using System.Net.Http;
  15. using System.Reflection;
  16. using XF.Domain.Dependency;
  17. using XF.Utility.EnumExtensions;
  18. namespace Hotline.Application.ExportExcel
  19. {
  20. public class ExportApplication : IExportApplication, IScopeDependency
  21. {
  22. private readonly IMapper _mapper;
  23. public ExportApplication(IMapper mapper)
  24. {
  25. _mapper = mapper;
  26. }
  27. /// <summary>
  28. /// 导出数据
  29. /// </summary>
  30. /// <typeparam name="T"></typeparam>
  31. /// <param name="list">数据集</param>
  32. /// <param name="name">导出文件名(不传则生成yyyyMMddhhmmss)</param>
  33. /// <returns></returns>
  34. public FileStreamResult ExportData<T>(IList<T> list, string? name)
  35. {
  36. var stream = new MemoryStream();
  37. stream.SaveAs(list);
  38. stream.Seek(0, SeekOrigin.Begin);
  39. return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
  40. {
  41. FileDownloadName = !string.IsNullOrEmpty(name) ? DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx" : name
  42. };
  43. }
  44. public Stream GetExcelStream<T, D>(ExportExcelDto<D> dto, IList<T> items, Func<IList<T>, T>? func = null)
  45. {
  46. if (items != null && items.Count > 0 && func != null)
  47. {
  48. items.Add(func.Invoke(items));
  49. }
  50. dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass(dto.ColumnInfos);
  51. var dtos = items
  52. .Select(stu => _mapper.Map(stu, typeof(T), dynamicClass))
  53. .Cast<object>()
  54. .ToList();
  55. return ExcelHelper.CreateStream(dtos);
  56. }
  57. public FileStreamResult GetExcelFile<T, D>(ExportExcelDto<D> dto, IList<T> items, string fileName, Func<IList<T>, T>? func = null)
  58. {
  59. return GetExcelStream(dto, items, func).GetExcelFile(fileName);
  60. }
  61. public FileStreamResult GetExcelFile<T, D>(ExportExcelDto<D> dto, IList<T> items, string fileName, string totalName) where T : new()
  62. {
  63. var fieldsAll = typeof(T).GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);
  64. var fields = fieldsAll.Where(f => IsNumericType(f.FieldType)); // 只选择数值类型字段
  65. var totalField = fieldsAll.Where(m => m.Name.Contains(totalName)).First();
  66. var sumDict = new Dictionary<FieldInfo, dynamic>();
  67. foreach (var field in fields)
  68. {
  69. sumDict[field] = 0;
  70. }
  71. foreach (var item in items)
  72. {
  73. foreach (var field in fields)
  74. {
  75. var value = field.GetValue(item);
  76. if (value != null)
  77. {
  78. sumDict[field] += (dynamic)value; // 使用 dynamic 累加
  79. }
  80. }
  81. }
  82. var resultItem = new T();
  83. totalField.SetValue(resultItem, "合计");
  84. foreach (var field in fields)
  85. {
  86. field.SetValue(resultItem, sumDict[field]);
  87. }
  88. items.Add(resultItem);
  89. return GetExcelFile(dto, items, fileName);
  90. }
  91. private static bool IsNumericType(Type type)
  92. {
  93. return type == typeof(int) || type == typeof(float) || type == typeof(double) ||
  94. type == typeof(decimal) || type == typeof(long) || type == typeof(short) ||
  95. type == typeof(byte) || type == typeof(uint) || type == typeof(ulong) ||
  96. type == typeof(ushort) || type == typeof(sbyte);
  97. }
  98. }
  99. }