public async Task <IActionResult> GetLaborDetailByHeadPage([FromQuery] GetLaborDetailViewModel model)
        {
            PageInfoHelper <LaborDetailListViewModel> result = await _laborHeadService.GetAllByHeadPage(model);

            Response.Headers.Add("Pagination-X", JsonSerializer.Serialize(result.TotalCount));
            return(Ok(result));
        }
        /// <summary>
        /// 通过劳保导出这期所有人的劳保excel
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public byte[] ExlExport(GetLaborDetailViewModel model)
        {
            var list = GetAllByHead(model).Select(m => new
            {
                m.Account,
                m.Department,
                m.Option,
                m.Goods
            }).ToList();
            IWorkbook workbook = new XSSFWorkbook();
            ISheet    sheet    = workbook.CreateSheet("sheet");
            IRow      titleRow = sheet.CreateRow(0);
            IRow      rows     = null;
            //获取访问属性
            Type entityType = list[0].GetType();

            PropertyInfo[] entityProperties = entityType.GetProperties();

            for (int i = 0; i <= list.Count; i++)
            {
                if (i == 0)
                {
                    //标题行
                    titleRow.CreateCell(0).SetCellValue("序号");
                    titleRow.CreateCell(1).SetCellValue("用户");
                    titleRow.CreateCell(2).SetCellValue("部门");
                    titleRow.CreateCell(3).SetCellValue("选项");
                    titleRow.CreateCell(4).SetCellValue("劳保");
                }
                else
                {
                    //正文行
                    rows = sheet.CreateRow(i);
                    object entity = list[i - 1];
                    //遍历列
                    for (int j = 1; j <= entityProperties.Length; j++)
                    {
                        //使用对象,不需要指定类型
                        object[] entityValues = new object[entityProperties.Length];
                        entityValues[j - 1] = entityProperties[j - 1].GetValue(entity) is null?"":entityProperties[j - 1].GetValue(entity);
                        rows.CreateCell(0).SetCellValue(i);
                        rows.CreateCell(j).SetCellValue(entityValues[j - 1].ToString());
                    }
                }
            }

            byte[] buffer = new byte[1024 * 2];
            using (MemoryStream ms = new MemoryStream())
            {
                workbook.Write(ms);
                buffer = ms.ToArray();
                ms.Close();
            }
            return(buffer);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// 获取劳保通过LaborId
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IQueryable <LaborDetailListViewModel> GetAllByHead(GetLaborDetailViewModel model)
        {
            IQueryable <LaborDetailListViewModel> result = from user in _context.User
                                                           join laborDetail in _context.LaborDetail.Where(m => m.LaborId == model.LaborId)
                                                           on user.Id equals laborDetail.UserId into userJoinLabor
                                                           from laborDetail in userJoinLabor.DefaultIfEmpty()
                                                           orderby laborDetail.Goods
                                                           select new LaborDetailListViewModel
            {
                Account      = user.UserName,
                Department   = user.Department.DeptName,
                DepartmentId = user.DepartmentId,
                Option       = laborDetail.Option,
                Goods        = laborDetail.Goods,
            };

            if (model.DeptId != Guid.Empty)
            {
                result = result.Where(m => m.DepartmentId == model.DeptId);
            }
            return(result);
        }
 public IActionResult ExportLabor([FromQuery] GetLaborDetailViewModel model)
 {
     return(File(_laborHeadService.ExlExport(model), "application/ms-excel", $"{model.Title}-{DateTime.Today}.xlsx"));
 }
 /// <summary>
 /// 获取劳保通过Id
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public IQueryable <LaborDetailListViewModel> GetAllByHead(GetLaborDetailViewModel model)
 {
     return(_laborDetailRepository.GetAllByHead(model));
 }
        /// <summary>
        /// 获取劳保通过LaborId分页
        /// </summary>
        /// <returns></returns>
        public async Task <PageInfoHelper <LaborDetailListViewModel> > GetAllByHeadPage(GetLaborDetailViewModel model)
        {
            IQueryable <LaborDetailListViewModel> result = _laborDetailRepository.GetAllByHead(model);
            var a = await PageInfoHelper <LaborDetailListViewModel> .CreatePageMsgAsync(result, model.PageNumber, model.PageSize);

            return(a);
        }