Excel_BusConfItemList.cs 3.6 KB

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