1、写Excel的三种方式

        // 注意 simpleWrite在数据量不大的情况下可以使用(5000以内,具体也要看实际情况),数据量大参照 重复多次写入

        // 写法1 JDK8+
        // since: 3.0.0-beta1
        String fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, DemoData.class)
            .sheet("模板")
            .doWrite(() -> {
                // 分页查询数据
                return data();
            });

        // 写法2
        fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写,然后写到第一个sheet,名字为模板 然后文件流会自动关闭
        // 如果这里想使用03 则 传入excelType参数即可
        EasyExcel.write(fileName, DemoData.class).sheet("模板").doWrite(data());

        // 写法3
        fileName = TestFileUtil.getPath() + "simpleWrite" + System.currentTimeMillis() + ".xlsx";
        // 这里 需要指定写用哪个class去写
        try (ExcelWriter excelWriter = EasyExcel.write(fileName, DemoData.class).build()) {
            WriteSheet writeSheet = EasyExcel.writerSheet("模板").build();
            excelWriter.write(data(), writeSheet);
        }

2、写一个Excel并设置样式(动态表头)

2.1 构建Excel并设置动态表头

        
            // 先写动态表头,注意:这里构建的是WritSheet对象,暂时未把excel写入response对象中
            WriteSheet writeSheet = EasyExcel.write()
                    // 设置sheet名称
                    .sheet("节点导出")
                    // 设置表头,这里调用一个方法,返回值为 List<List<String>>
                    .head(getHeadList(nodeEntityIdList, dataObjectList, relationNameList))
                    .build();
                    
 /**
  * 
  * 可以在这个方法中传入参数,进行动态表头的生成
  *
 **/
private static List<List<String>> getHeadList() {
        // 创建默认表头集合
        List<List<String>> headList = new ArrayList<>();
        // 第一列表头
        headList.add(ListUtil.toList("序号"));
        // 第二列表头
        headList.add(ListUtil.toList("实例"));
        // 第三列表头
        headList.add(ListUtil.toList("概念"));
        // 第n列表头
        ...
        
        return headList;
    }

2.2 设置样式

            // 设置自动换行
            val contentWriteCellStyle = new WriteCellStyle();
            // 文本自动换行
            contentWriteCellStyle.setWrapped(true);
            // 水平对齐
            contentWriteCellStyle.setHorizontalAlignment(HorizontalAlignment.CENTER);
            // 垂直对齐
            contentWriteCellStyle.setVerticalAlignment(VerticalAlignment.CENTER);
            // 构建Excel,这里写入response对象中
            ExcelWriter writer = EasyExcel.write(response.getOutputStream())
                    // 设置固定列宽
                    .registerWriteHandler(new SimpleColumnWidthStyleStrategy(30))
                    // 注册WriteCellStyle对象
                    .registerWriteHandler(new HorizontalCellStyleStrategy(null,contentWriteCellStyle))
                    // 合并前三列 序号、实例、概念
                    .registerWriteHandler(new ExcelMergeUtil(1, new int[]{0, 1, 2}))
                    // 合并数据属性列,动态列数
                    .registerWriteHandler(new ExcelMergeUtil(1, b))
                    .build();

2.3 完成写入excel

            // 这里调用ExcelWriter对象,写入数据,
            // 参数一为写入的数据类型为List<List<String>> rowList = new ArrayList<>();
            // 参数二为一开始写动态表头时构建的WriteSheet对象
            writer.write(rowList, writeSheet);
            // 结束写入
            writer.finish();
            writer.close();

2.4 导出excel

             // 设置响应头
            response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
            response.setCharacterEncoding("utf-8");
            // 文件名称
            String fileName = URLEncoder.encode("测试下载", "UTF-8").replaceAll("\\+", "%20");
            response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");