You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
2.6 KiB
68 lines
2.6 KiB
using System;
|
|
using System.Collections.Generic;
|
|
using System.IO;
|
|
using System.Linq;
|
|
using System.Linq.Dynamic.Core;
|
|
using System.Threading.Tasks;
|
|
using SqlSugar;
|
|
using Tiobon.Core.Common;
|
|
using Tiobon.Core.Common.DB;
|
|
using Tiobon.Core.Common.Extensions;
|
|
using Tiobon.Core.Model.Models;
|
|
namespace Tiobon.Core.DataAccess;
|
|
|
|
/// <summary>
|
|
/// 报表辅助类
|
|
/// </summary>
|
|
public static class ReportHelper
|
|
{
|
|
|
|
#region 文件下载
|
|
/// <summary>
|
|
/// 生成文件,并通知用户下载(IQueryable)
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
/// <param name="list"></param>
|
|
/// <param name="modelName">导出模块名称(JobSetting.xxx)</param>
|
|
/// <param name="exportFields"></param>
|
|
/// <param name="exportFieldsWidth"></param>
|
|
/// <param name="user"></param>
|
|
/// <param name="sort"></param>
|
|
/// <param name="headText"></param>
|
|
/// <param name="totalText"></param>
|
|
/// <param name="isNeedItemNo"></param>
|
|
public static async Task<long> SendFile<T>(IQueryable<T> list, string modelName, List<string> exportFields, List<int> exportFieldsWidth, string sort = null, string headText = "", string totalText = "", bool isNeedItemNo = false) where T : class
|
|
{
|
|
|
|
if (list == null)
|
|
throw new Exception("生成文件失败");
|
|
//生成文件至文件服务器
|
|
var fid = SnowFlakeSingle.Instance.NextId();
|
|
var path = $"{$"{Environment.CurrentDirectory}{Path.DirectorySeparatorChar}wwwroot{Path.DirectorySeparatorChar}files{Path.DirectorySeparatorChar}export{Path.DirectorySeparatorChar}{fid}{Path.DirectorySeparatorChar}"}";
|
|
if (!Directory.Exists(path))
|
|
Directory.CreateDirectory(path);
|
|
var fname = $"{modelName}.xlsx";
|
|
|
|
list = string.IsNullOrEmpty(sort) ? list : list.OrderBy(sort);
|
|
list.IntoFileFromLinqExcel(path + fname, ",", exportFields, exportFieldsWidth, headText, totalText, isNeedItemNo);
|
|
|
|
using var _context = ContextFactory.CreateContext();
|
|
|
|
Ghre_Attachment fileAttachment = new Ghre_Attachment();
|
|
fileAttachment.Id = fid;
|
|
fileAttachment.AttachFileName = fname;
|
|
fileAttachment.CreateBy = App.User.ID;
|
|
fileAttachment.CreateTime = DateTime.Now;
|
|
fileAttachment.AttachmentName = fname;
|
|
fileAttachment.AttachFileExtension = "xlsx";
|
|
fileAttachment.AttachFileSize = 0;
|
|
fileAttachment.PhysicsPath = $"/files/export/{fid}/" + fname;
|
|
fileAttachment.AttachmentType = "xlsx";
|
|
|
|
await _context.Ghre_Attachment.AddAsync(fileAttachment);
|
|
await _context.SaveChangesAsync();
|
|
return fid;
|
|
}
|
|
#endregion
|
|
|
|
} |