
using Microsoft.AspNetCore.Mvc;
using MiniExcelLibs;
using XF.Domain.Dependency;

namespace Hotline.Application.ExportExcel
{
    public class ExportApplication : IExportApplication, IScopeDependency
    {
        /// <summary>
        /// 导出数据
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="list">数据集</param>
        /// <param name="name">导出文件名（不传则生成yyyyMMddhhmmss）</param>
        /// <returns></returns>
        public FileStreamResult ExportData<T>(List<T> 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
            };
        }
    }
}
