using Hotline.Application.Tools;
using Hotline.Share.Dtos.CallCenter;
using Hotline.Share.Dtos.Order;
using Hotline.Share.Enums.Article;
using Hotline.Share.Tools;
using Hotline.Tools;
using Mapster;
using MapsterMapper;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.DependencyInjection;
using MiniExcelLibs;
using System.Reflection;
using XF.Domain.Dependency;
using XF.Utility.EnumExtensions;
namespace Hotline.Application.ExportExcel
{
public class ExportApplication : IExportApplication, IScopeDependency
{
private readonly IMapper _mapper;
private readonly IHttpContextAccessor _httpContextAccessor;
public ExportApplication(IMapper mapper, IHttpContextAccessor httpContextAccessor)
{
_mapper = mapper;
_httpContextAccessor = httpContextAccessor;
}
///
/// 导出数据
///
///
/// 数据集
/// 导出文件名(不传则生成yyyyMMddhhmmss)
///
public FileStreamResult ExportData(IList list, string? name)
{
var stream = new MemoryStream();
stream.SaveAs(list);
stream.Seek(0, SeekOrigin.Begin);
return new FileStreamResult(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
{
FileDownloadName = !string.IsNullOrEmpty(name) ? DateTime.Now.ToString("yyyyMMddhhmmss") + ".xlsx" : name
};
}
public Stream GetExcelStream(ExportExcelDto dto, IList items, Func, T>? func = null)
{
if (items != null && items.Count > 0 && func != null)
{
items.Add(func.Invoke(items));
}
dynamic? dynamicClass = DynamicClassHelper.CreateDynamicClass(dto.ColumnInfos);
var dtos = items
.Select(stu => _mapper.Map(stu, typeof(T), dynamicClass))
.Cast