ef core 导出excel demo

 /// <summary>
 /// 导出表
 /// </summary>
 /// <returns></returns>
 [AllowAnonymous]
 [HttpGet, Route("GeExcel/{KeyID}"), Route("GeExcel")]
 public FileContentResult GeExcel(string KeyID)
 {
     BaseResult result = new BaseResult();
     try
     {
         Dictionary<string, string> titles = new Dictionary<string, string>();
         titles = new Dictionary<string, string>
         {
             { "ID", "序号" },
             { "PurchaseBatch", "采购批次" },
             { "CategoryMaxName", "物资种类" },
             { "CategoryName", "物资品类" },
             { "Name", "物资名称" },
             { "Type", "品牌型号" },
             { "Model", "规格" },
             { "ActualNum", "数量" },
             { "ProductionDate","生产日期"},
             { "SupplierName","供应商"},
             { "DeadlineDate","临期时间"},
             { "QualityDate","有效期"},
             { "FromName","业务类型"},
             { "binno","存放位置"},
             { "CreateDate","出入库时间"},
             { "TaskCode","出入库单号"},
             { "CblxName","储备性质"},
             { "Owner","权属单位"}        
         };
         string ProjectCode = "620010001";          
         List<string> ProjectList = _common.GetNowAllProject(ProjectCode);
         var list = from a in _context.wuzi_outinrecord
                    join b in _context.wuzi_outinrecorditem on a.ID equals b.RecordID
                    join c in _context.wuzi_variety on b.VarietyCode equals c.Code
                    join i in _context.wuzi_varietybatch on b.BatchNum equals i.BatchNum
                    join f in _context.wuzi_category on c.Category equals f.Code
                    join h in _context.sys_dictionary on c.Unit equals h.Value
                    join s in _context.wuzi_supplier on c.SupplierId equals s.ID into aa
                    from e in aa.DefaultIfEmpty()
                    where a.Deleted == BaseConsts.未删除 && ProjectList.Contains(a.ProjectCode)
                    && a.IsExecuted > 0 && h.TypeCode == "020" && b.VarietyCode != "BPALLET-T" && c.Code != "BPALLET-T" && b.ActualNum != 0 && b.ActualNum != null
                     && (b.OutInType == "01" || b.OutInType == "02" || b.OutInType == "08" || b.OutInType == "18" || b.OutInType == "16" || b.OutInType == "17")
                    select new
                    {
                        a.ID,
                        a.StoreHouseCode,
                        a.CreateDate,
                        DocType = a.Type,
                        FromName = _context.sys_dictionary.Where(x => x.TypeCode == a.Type && x.Value == a.DetailType).FirstOrDefault().Name,
                        CategoryMaxName = _context.wuzi_category.FirstOrDefault(a => a.Code == _context.wuzi_category.FirstOrDefault(a => a.Code == f.ParentCode).ParentCode).Name, // 物资种类(大类)
                        CategoryMaxCode = _context.wuzi_category.FirstOrDefault(a => a.Code == f.ParentCode).ParentCode, // 物资种类(大类)
                        CategoryName = f.Name,
                        CategoryCode = f.Code,
                        c.Name,
                        c.Code,
                        c.Type,
                        c.Model,
                        c.PurchaseBatch,
                        c.ProductionDate,
                        c.Validity,
                        ActualNum = b.ActualNum == null ? 0 : decimal.Parse(b.ActualNum.ToString().Replace("-", "")),
                        a.TaskCode,
                        CblxName = _context.sys_dictionary.Where(x => x.TypeCode == "041" && x.Value == b.Cblx).FirstOrDefault().Name,
                        a.Owner,
                        SupplierName = e.Name,
                        c.Remark
                    };
         if (!string.IsNullOrEmpty(KeyID))
         {
             List<int> BatchId = KeyID.Split(',').Select(a => int.Parse(a)).ToList();
             list = list.Where(a => BatchId.Contains(a.ID));
         }
         // recordid:订单编号 orderlineid:订单明细编号 materno:物资编号 palletno:托盘编号
         var WmsBack = _context.wuzi_wmsfeedbackdetaillog.GroupBy(x => new { x.recordid, x.orderlineid, x.materno, x.palletno, x.binno, x.location })
                                                          .Select(xx => new { xx.Key.recordid, xx.Key.materno, xx.Key.binno, xx.Key.location }).ToList();

         var OutInRecordList = list.ToList();
         var RealList = new List<dynamic>();
         foreach (var i in OutInRecordList)
         {
             var ResultModel = new
             {
                 i.ID,  // 序号
                 i.PurchaseBatch, // 采购批次
                 i.CategoryMaxName, // 物资种类(大类)
                 i.CategoryName, // 物资品类(小类)
                 i.Name, // 物资名称
                 i.Type, // 品牌型号
                 i.Model, // 包装规格
                 i.SupplierName, // 供应商名称
                 i.ProductionDate, // 生产日期
                 i.FromName,      // 业务类型
                 binno = WmsBack.Where(x => x.recordid == i.ID.ToString() && x.materno == i.Code).FirstOrDefault()?.location ?? "", // 存放位置
                 QualityDate = i.ProductionDate != null ? Convert.ToDateTime(i.ProductionDate).AddDays(double.Parse(i.Validity == null ? "0" : i.Validity) * 365.25).ToString("yyyy-MM-dd") : null,  // 有效期
                 DeadlineDate = i.ProductionDate != null ? Convert.ToDateTime(i.ProductionDate).AddDays(double.Parse(i.Validity == null ? "0" : i.Validity) * 365.25).AddMonths(-1).ToString("yyyy-MM-dd") : null, // 临期时间
                 i.ActualNum, // 数量
                 i.TaskCode,  // 出入库交接单号
                 i.CreateDate,   // 出入库时间
                 i.CblxName,   // 储备性质
                 i.Owner,     // 权属单位
             };
             RealList.Add(ResultModel);
         };
         var fileName = "xxxxxx.xls";
         DataTable datatable = NpoiHelpers.ListToDataTables(RealList);
         var stream = NpoiHelpers.ExportExcels(datatable, titles, "", fileName);
         byte[] bytes = new byte[stream.Length];
         stream.Read(bytes, 0, bytes.Length);
         // 设置当前流的位置为流的开始
         stream.Seek(0, SeekOrigin.Begin);
         return new FileContentResult(bytes, "application/vnd.ms-excel")
         {
             FileDownloadName = fileName
             //如果设置了FileDownloadName ,则浏览器会直接下载该文件,不设置则浏览器会直接显示图片
         };
     }
     catch (Exception ex)
     {
         result = null;
         result.code = (int)Const.c_code.抛出异常;
         result.message = Const.c_code.抛出异常.ToString();
         Log.CreateLog(Log.LOG_ERROR, _getuserdata.UserName, "GetMaterialOutInDetailExcel", ex.Message);
     }
     return null;
 }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

瓜皮弟子头很铁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值