using Magicodes.ExporterAndImporter.Core; using Magicodes.ExporterAndImporter.Excel; using SqlSugar; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using WIDESEA_Core; using WIDESEA_Core.BaseServices; using WIDESEA_Core.Helper; using WIDESEA_IStockRepository; using WIDESEA_IStockService; using WIDESEA_Model.Models; namespace WIDESEA_StockService { public partial class VV_StockInfoDetService : ServiceBase, IVV_StockInfoDetService { //导出 public override WebResponseContent Export(PageDataOptions options) { WebResponseContent content = new WebResponseContent(); try { string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelExport"; IExporter exporter = new ExcelExporter(); //添加条件 string wheres = options.ValidatePageOptions(TProperties); //获取排序字段 Dictionary orderbyDic = options.GetPageDataSort(TProperties); List entities = BaseDal.QueryData(wheres, orderbyDic); var stockdct = entities .GroupBy(x => new { x.MaterielCode, x.MaterialType, x.MaterielName, x.OrderNo, x.BatchNo, x.BatchNoName }) .Select(g => new VV_StockInfoDet { MaterielCode = g.Key.MaterielCode, MaterialType = g.Key.MaterialType.ToString()=="0"?"原材料":"成品", MaterielName = g.Key.MaterielName, OrderNo = g.Key.OrderNo, BatchNo = g.Key.BatchNo, BatchNoName = g.Key.BatchNoName, StockQuantity = g.Sum(x => x.StockQuantity), CreateDate = g.Min(x => x.CreateDate) // 获取每组的最早 CreateDate }) .ToList(); byte[] data = exporter.ExportAsByteArray(stockdct).Result; string fileName = "1.xlsx"; FileHelper.WriteFile(savePath, fileName, data); content = WebResponseContent.Instance.OK(data: savePath + "\\" + fileName); } catch (Exception ex) { content = WebResponseContent.Instance.Error(ex.Message); } return content; } } }