jeecgboot 自定义导出字段

发布时间 2023-09-09 10:53:23作者: 卡农的忧伤ろ◆
实体类
@Data
public class Cost implements Serializable {

    /**主键*/
    @ApiModelProperty(value = "主键")
    private String id;

    /**日期*/
    @Excel(name = "日期", format = "yyyy-MM-dd")
    @JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd")
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @ApiModelProperty(value = "日期")
    private Date time;/**成本*/
    @Excel(name = "成本")
    @ApiModelProperty(value = "成本")
    private String totalCost;
}

导出方法

@RequestMapping(value = "/exportXls")
public ModelAndView exportXls(HttpServletRequest request, Cost Cost) {
   // 导出的字段
   String[] exportFields = {"time"};
   return this.exportXls(request, Cost, Cost.class, "成本管理表", exportFields);
}

导出Excel方法

/**
      * 导出excel
      *
      * @param request
      */
     protected ModelAndView exportXls(HttpServletRequest request, Cost object, Class<Cost> clazz, String title, String[] exportFields) {
         // Step.1 组装查询条件
         QueryWrapper<Cost> queryWrapper = QueryGenerator.initQueryWrapper(object, request.getParameterMap());
         LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();

         // Step.2 获取导出数据
         List<Cost> pageList = costService.list(queryWrapper);
         List<Cost> exportList = null;

         // 过滤选中数据
         String selections = request.getParameter("selections");
         if (oConvertUtils.isNotEmpty(selections)) {
             List<String> selectionList = Arrays.asList(selections.split(","));
             exportList = pageList.stream().filter(item -> selectionList.contains(getId(item))).collect(Collectors.toList());
         } else {
             exportList = pageList;
         }

         // Step.3 AutoPoi 导出Excel
         ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
         mv.addObject(NormalExcelConstants.FILE_NAME, title); //此处设置的filename无效 ,前端会重更新设置一下
         mv.addObject(NormalExcelConstants.CLASS, clazz);
         mv.addObject(NormalExcelConstants.PARAMS, new ExportParams(title + "报表", "导出人:" + sysUser.getRealname(), title));
         mv.addObject(NormalExcelConstants.DATA_LIST, exportList);
         // 导出的字段
         mv.addObject(NormalExcelConstants.EXPORT_FIELDS, StringUtils.join(exportFields, ","));
         return mv;
     }

     /**
      * 获取对象ID
      *
      * @return
      */
     private String getId(Cost item) {
         try {
             return PropertyUtils.getProperty(item, "id").toString();
         } catch (Exception e) {
             e.printStackTrace();
             return null;
         }
     }