easypoi导出动态表头excel

1,011 阅读1分钟

最近遇到一个业务,列表表头不定,导出逻辑不能使用定义对象的方式实现导出

引入maven依赖

<dependency>
         <groupId>cn.afterturn</groupId>
         <artifactId>easypoi-spring-boot-starter</artifactId>
         <version>4.1.2</version>
  </dependency>
  

controller层

@GetMapping("/xxxExport")
@ApiOperation(value = "导出", notes = "导出")
public void report2MonthExport(List<Map<String, Object>> result, String type, HttpServletResponse response) throws Exception {
    Workbook workbook = xxxService.dynaCol( result, type);
    ExcelUtil.downloadExcel("分时段列表.xls",workbook,response);
}

service 层

private Workbook dynaCol(List<Map<String, Object>> result, String type) {
    List<Map<String, Object>> tableHeader = tableHeader(type);
    List<ExcelExportEntity> colList = new ArrayList<>();
    Map<String, String> childrenToParentMap = new HashMap<>();
    //表头组装
    for (Map<String, Object> stringObjectMap : tableHeader) {
        String label = String.valueOf(stringObjectMap.get(LABEL));
        String pros = String.valueOf(stringObjectMap.get(PROS));
        Object children = stringObjectMap.get(CHILDREN);
        if (children instanceof List) {
            ExcelExportEntity colEntity = new ExcelExportEntity(label, PinYinUtil.ToPinyin(label));
            List<Map> childrenMapList = (List<Map>) children;
            List<ExcelExportEntity> deliColList = new ArrayList<>();
            for (Map childMap : childrenMapList) {
                String childrenLabel = String.valueOf(childMap.get(LABEL));
                String childrenPros = String.valueOf(childMap.get(PROS));
                deliColList.add(new ExcelExportEntity(childrenLabel, childrenPros));
                childrenToParentMap.put(childrenPros, label);
            }
            colEntity.setList(deliColList);
            colList.add(colEntity);
        } else {
            ExcelExportEntity colEntity = new ExcelExportEntity(label, pros);
            colEntity.setNeedMerge(true);
            colList.add(colEntity);
        }

    }
    //数据组装
    List<Map<String, Object>> list = new ArrayList<>();
    Map<String,Map<String,Object>> parentMap;
    for (Map<String, Object> dataMap : result) {
        Map<String, Object> valMap = new HashMap<>();
        Set<Map.Entry<String, Object>> dataEntries = dataMap.entrySet();
        parentMap = new HashMap<>();
        for (Map.Entry<String, Object> dataEntry : dataEntries) {
            String key = dataEntry.getKey();
            if (TIME_KEY.equals(key) || SPACE_NAME_KEY.equals(key)) {
                valMap.put(key, dataEntry.getValue());
            }else{
                String parentKey = PinYinUtil.ToPinyin(String.valueOf(childrenToParentMap.get(key)));
                if (!parentMap.containsKey(parentKey)) {
                    parentMap.put(parentKey, new HashMap<>());
                }
                parentMap.get(parentKey).put(dataEntry.getKey(), dataEntry.getValue());
            }
        }

        Set<Map.Entry<String, Map<String, Object>>> entries = parentMap.entrySet();
        for (Map.Entry<String, Map<String, Object>> entry : entries) {
            String key = entry.getKey();
            List<Map<String, Object>> periodMapList = new ArrayList<>();
            periodMapList.add(entry.getValue());
            valMap.put(key, periodMapList);
        }

        list.add(valMap);

    }
    //根据表头和数据集,返回对应结果
   // return ExcelExportUtil.exportExcel(new ExportParams("数据分析","sheet0"), colList, list);
    return ExcelExportUtil.exportExcel(new ExportParams(), colList, list);
}

new ExportParams("数据分析","sheet0") 可定义为sheet名称和excel大标题

效果

image.png

参考

https://www.cnblogs.com/dw3306/p/12416843.html