BillInventoryReportController.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using Microsoft.AspNetCore.Mvc;
  2. using Newtonsoft.Json;
  3. using System.Data;
  4. using System.Web;
  5. using WMS.BZModels.Dto.CP.BillInventoryReportDtos;
  6. using WMS.BZServices.CP;
  7. using WMS.Info;
  8. using WMS.Util;
  9. namespace WMS.BZWeb.Areas.CPManager.Controllers
  10. {
  11. [Area("CPManager")]
  12. public class BillInventoryReportController : MvcControllerBase
  13. {
  14. private readonly BillInventoryReportService _billInventoryReportService;
  15. public BillInventoryReportController(BillInventoryReportService billInventoryReportService)
  16. {
  17. _billInventoryReportService = billInventoryReportService;
  18. }
  19. public IActionResult Index()
  20. {
  21. return View();
  22. }
  23. public ActionResult GetPageList(string pagination, string queryJson)
  24. {
  25. Pagination paginationobj = InitPagination(pagination);
  26. var query = new BillInventoryReportQueryDto();
  27. if (!string.IsNullOrEmpty(queryJson))
  28. {
  29. query = JsonConvert.DeserializeObject<BillInventoryReportQueryDto>(queryJson);
  30. }
  31. var lists = _billInventoryReportService.GetPageList(paginationobj, query ?? new BillInventoryReportQueryDto());
  32. var jsonData = new
  33. {
  34. rows = lists.Result,
  35. total = lists.TotalPage,
  36. page = lists.PageIndex,
  37. records = lists.TotalNum
  38. };
  39. return Success(jsonData);
  40. }
  41. [HttpPost]
  42. public IActionResult ExportExcel(string fileName, string queryJson, string exportField)
  43. {
  44. var query = new BillInventoryReportQueryDto();
  45. try
  46. {
  47. if (!string.IsNullOrEmpty(queryJson))
  48. {
  49. query = JsonConvert.DeserializeObject<BillInventoryReportQueryDto>(queryJson);
  50. }
  51. }
  52. catch (Exception ex)
  53. {
  54. }
  55. //设置导出格式
  56. ExcelConfig excelconfig = new ExcelConfig();
  57. excelconfig.Title = HttpUtility.UrlDecode(fileName);
  58. excelconfig.TitleFont = "微软雅黑";
  59. excelconfig.TitlePoint = 15;
  60. excelconfig.FileName = HttpUtility.UrlDecode(fileName) + ".xls";
  61. excelconfig.IsAllSizeColumn = true;
  62. excelconfig.ColumnEntity = new List<ColumnModel>();
  63. List<jfGridModel> columnList = exportField.ToList<jfGridModel>();
  64. if (columnList.Count == 0)
  65. {
  66. throw new Exception("未找到表头");
  67. }
  68. //行数据
  69. var lists = _billInventoryReportService.GetList(query ?? new BillInventoryReportQueryDto());
  70. var dataJson = JsonConvert.SerializeObject(lists);
  71. DataTable rowData = dataJson.ToTable();
  72. //写入Excel表头
  73. Dictionary<string, string> exportFieldMap = new Dictionary<string, string>();
  74. if (!string.IsNullOrEmpty(exportField))
  75. {
  76. string[] exportFields = exportField.Split(',');
  77. foreach (var field in exportFields)
  78. {
  79. if (!exportFieldMap.ContainsKey(field))
  80. {
  81. exportFieldMap.Add(field, "1");
  82. }
  83. }
  84. }
  85. foreach (var columnModel in columnList)
  86. {
  87. excelconfig.ColumnEntity.Add(new ColumnModel()
  88. {
  89. // Column =string.IsNullOrEmpty(columnModel.sortname)? columnModel.name: columnModel.sortname,
  90. Column = columnModel.name,
  91. ExcelColumn = columnModel.label,
  92. Alignment = columnModel.align,
  93. Width = columnModel.name.Length
  94. });
  95. }
  96. return File(ExcelHelper.NewExportMemoryStream(rowData, excelconfig), "application/ms-excel", excelconfig.FileName);
  97. }
  98. }
  99. }