Excel_BusConfItemList.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. using Aspose.Cells;
  2. using OASystem.Domain.Entities.Business;
  3. using OASystem.Domain.Entities.Groups;
  4. using OASystem.Domain.ViewModels.Business;
  5. using OASystem.Infrastructure.Repositories.Business;
  6. using StackExchange.Redis;
  7. using System.Web;
  8. namespace OASystem.API.OAMethodLib.ExcelOutput
  9. {
  10. public class Excel_BusConfItemList
  11. {
  12. private CommonBusRepository _busRep;
  13. private string url;
  14. private string path;
  15. public Excel_BusConfItemList()
  16. {
  17. _busRep = AutofacIocManager.Instance.GetService<CommonBusRepository>();
  18. url = AppSettingsHelper.Get("ExcelBaseUrl");
  19. path = AppSettingsHelper.Get("ExcelBasePath");
  20. if (!System.IO.Directory.Exists(path))
  21. {
  22. System.IO.Directory.CreateDirectory(path);//不存在就创建文件夹
  23. }
  24. }
  25. public string Excel(Bus_ConfItemListInfo _entity)
  26. {
  27. if (!string.IsNullOrEmpty(_entity.ExcelPath))
  28. {
  29. if (System.IO.File.Exists(_entity.ExcelPath))
  30. {
  31. //存在
  32. string fileUrl = url + path + _entity.ExcelPath;
  33. return fileUrl;
  34. }
  35. else
  36. {
  37. //不存在
  38. return CreateExcel(_entity);
  39. }
  40. }
  41. else
  42. {
  43. return CreateExcel(_entity);
  44. }
  45. return "";
  46. }
  47. private string CreateExcel(Bus_ConfItemListInfo _entity)
  48. {
  49. string sql = string.Format(@" Select * From(
  50. Select ROW_NUMBER() Over(order By Id desc) as RowNumber,riv.FullName as VendorName,rid.ItemName
  51. ,rit.TypeName as ItemTypeName,bci.[Count],bci.CurrCost as Price,bci.OpRemark as ItemRemark
  52. From Bus_ConfItemList as bcil With(Nolock)
  53. Inner Join Bus_ConfItem as bci With(Nolock) On bcil.Id=bci.ConfListId
  54. Inner Join Res_ItemDetail as rid With(Nolock) On bci.ItemId=rid.Id
  55. Inner Join Res_ItemType as rit With(Nolock) On rid.ItemTypeId=rit.Id
  56. Inner Join Res_ItemVendor as riv With(nolock) On rid.VendorId=riv.Id
  57. Where bcil.Id={0}
  58. ) as tb ", _entity.Id);
  59. List<Bus_ConfItemExportView> _confItemList = _busRep._sqlSugar.SqlQueryable<Bus_ConfItemExportView>(sql).ToList();
  60. Grp_DelegationInfo _DelegationInfo = _busRep.Query<Grp_DelegationInfo>(s => s.Id == _entity.Diid).First();
  61. string diName = _DelegationInfo != null ? _DelegationInfo.TeamName : "未知团组" + _entity.Diid;
  62. WorkbookDesigner designer = new WorkbookDesigner();
  63. designer.Workbook = new Workbook(AppSettingsHelper.Get("ExcelBasePath") + "Template/会务物料清单模板.xlsx");
  64. designer.SetDataSource("Export", _confItemList);
  65. designer.SetDataSource("ExportDiName", diName);
  66. designer.SetDataSource("ExportOutputDt", DateTime.Now.ToString("yyyy-MM-dd"));
  67. designer.SetDataSource("ExportListRemark", _entity.Remark);
  68. designer.Process();
  69. string fileName = HttpUtility.UrlEncode("") + "_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xlsx";
  70. designer.Workbook.Save(fileName);
  71. designer = null;
  72. _entity.ExcelPath = path + fileName;
  73. _busRep._sqlSugar.Updateable<Bus_ConfItemListInfo>()
  74. .Where(s => s.Id == _entity.Id)
  75. .UpdateColumns(s => s.ExcelPath)
  76. .ExecuteCommand();
  77. string fileUrl = url + path + _entity.ExcelPath;
  78. return fileUrl;
  79. }
  80. }
  81. }