EasyExcel custom export columns and order

EasyExcel custom export columns and order

This function requires the connection between the front and back ends. The front end needs to transmit two collections, and the back end can be encapsulated into an object for receiving

Collection 1: List headList
insert image description here
Collection 2: List columnList [fields that need to be exported corresponding to the backend entity class]
insert image description here

code block

The front end can choose the number and order of columns exported

insert image description here

1. Entity class

	@Data
	public class ScenicAnalysis extends BaseEntity{
    
    
	    private static final long serialVersionUID = 1L;
	    
	    //需要导出的列的集合
	    private List<String> columnList;
	
	    //需要导出的列的集合
	    private List<String> headList;
	}

2. Control layer

    @PostMapping("/exportColumn")
    public void exportColumn(HttpServletResponse response, @RequestBody ScenicAnalysis scenicAnalysis) {
    
    
        scenicAnalysisService.exportColumn(response,scenicAnalysis);
    }

3. Method layer

    public void  exportColumn(HttpServletResponse response, ScenicAnalysis scenicAnalysis) {
    
    
        //根据自己的业务逻辑生成对应的List<Object>类型的集合
        List<Object> list = new ArrayList<>();
        try {
    
    
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            //控制 导出字段 未控制顺序
            EasyExcel.write(response.getOutputStream(), ScenicAnalysis.class)
                    .head(head(scenicAnalysis.getHeadList()))
                    .includeColumnFiledNames(scenicAnalysis.getColumnList())
                    .sheet("模板")
                    .doWrite(dataList(list,scenicAnalysis.getColumnList()));
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }

4. Public methods (direct use)

4.1 Setting header

    /**
   * 设置Excel头
   * @param headList  Excel头信息
   * @return
   */
  private static List<List<String>> head(List<String> headList) {
    
    
      List<List<String>> list = new ArrayList<>();
      for (String value : headList) {
    
    
          List<String> head = new ArrayList<>();
          head.add(value);
          list.add(head);
      }
      return list;
  }

4.2 Setting table information

  /**
  * 设置表格信息
  * @param dataList  查询出的数据
  * @param fileList  需要显示的字段
  * @return
  */
 private static List<List<Object>> dataList(List<Object> dataList, List<String> fileList) {
    
    
     List<List<Object>> list = new ArrayList<>();
     for (Object person : dataList) {
    
    
         List<Object> data = new ArrayList<>();
         for (String fieldName : fileList) {
    
    
             /**通过反射根据需要显示的字段,获取对应的属性值*/
             data.add(getFieldValue(fieldName, person));
         }
         list.add(data);
     }
     return list;
 }

4.3 Generate the corresponding get method according to the field

    /**
   * 根据传入的字段获取对应的get方法,如name,对应的getName方法
   * @param fieldName  字段名
   * @param person    对象
   * @return
   */
  private static Object getFieldValue(String fieldName, Object person) {
    
    
      try {
    
    
          String firstLetter = fieldName.substring(0, 1).toUpperCase();
          String getter = "get" + firstLetter + fieldName.substring(1);
          Method method = person.getClass().getMethod(getter);
          return method.invoke(person);
      } catch (Exception e) {
    
    
          return null;
      }
  }

The above can be adjusted according to your business needs

export results

insert image description here

Guess you like

Origin blog.csdn.net/nonita/article/details/129176195