using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using System.Data; using System.Web; using WMS.BZModels.Dto.CP.BillInventoryReportDtos; using WMS.BZServices.CP; using WMS.Info; using WMS.Util; namespace WMS.BZWeb.Areas.CPManager.Controllers { [Area("CPManager")] public class BillInventoryReportController : MvcControllerBase { private readonly BillInventoryReportService _billInventoryReportService; public BillInventoryReportController(BillInventoryReportService billInventoryReportService) { _billInventoryReportService = billInventoryReportService; } public IActionResult Index() { return View(); } public ActionResult GetPageList(string pagination, string queryJson) { Pagination paginationobj = InitPagination(pagination); var query = new BillInventoryReportQueryDto(); if (!string.IsNullOrEmpty(queryJson)) { query = JsonConvert.DeserializeObject(queryJson); } var lists = _billInventoryReportService.GetPageList(paginationobj, query ?? new BillInventoryReportQueryDto()); var jsonData = new { rows = lists.Result, total = lists.TotalPage, page = lists.PageIndex, records = lists.TotalNum }; return Success(jsonData); } [HttpPost] public IActionResult ExportExcel(string fileName, string queryJson, string exportField) { var query = new BillInventoryReportQueryDto(); try { if (!string.IsNullOrEmpty(queryJson)) { query = JsonConvert.DeserializeObject(queryJson); } } catch (Exception ex) { } //设置导出格式 ExcelConfig excelconfig = new ExcelConfig(); excelconfig.Title = HttpUtility.UrlDecode(fileName); excelconfig.TitleFont = "微软雅黑"; excelconfig.TitlePoint = 15; excelconfig.FileName = HttpUtility.UrlDecode(fileName) + ".xls"; excelconfig.IsAllSizeColumn = true; excelconfig.ColumnEntity = new List(); List columnList = exportField.ToList(); if (columnList.Count == 0) { throw new Exception("未找到表头"); } //行数据 var lists = _billInventoryReportService.GetList(query ?? new BillInventoryReportQueryDto()); var dataJson = JsonConvert.SerializeObject(lists); DataTable rowData = dataJson.ToTable(); //写入Excel表头 Dictionary exportFieldMap = new Dictionary(); if (!string.IsNullOrEmpty(exportField)) { string[] exportFields = exportField.Split(','); foreach (var field in exportFields) { if (!exportFieldMap.ContainsKey(field)) { exportFieldMap.Add(field, "1"); } } } foreach (var columnModel in columnList) { excelconfig.ColumnEntity.Add(new ColumnModel() { // Column =string.IsNullOrEmpty(columnModel.sortname)? columnModel.name: columnModel.sortname, Column = columnModel.name, ExcelColumn = columnModel.label, Alignment = columnModel.align, Width = columnModel.name.Length }); } return File(ExcelHelper.NewExportMemoryStream(rowData, excelconfig), "application/ms-excel", excelconfig.FileName); } } }