摘要
不需要传统的各种excel的创建工具比较繁琐,
场景:直接导出数据库查询数据 生成一个EXCEL自动创建多个sheet ,百万数据分组生成对应的数据表格。
细节内容都是参照
easyExcel官网文档
都可以实现,你想要的操作都有,
重要的事情说三遍:
一定要写与尝试
一定要写与尝试
一定要写与尝试
引入依赖
新版本的跟历史版本的API差了很多一定要1.xx的版本有他们自己的写法
<!-- https://mvnrepository.com/artifact/com.alibaba/easyexcel -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.6</version>
</dependency>
代码部分
@Test
public void testWrite(){
String fileName = "D:\\logs\\new\\testHead1.xlsx";
ExcelWriter excelWriter = null;
List<List<String>> fyList = new ArrayList<>();
List<List<String>> heads = Lists.newArrayList();
//表格数据 10条
for(int i =0 ;i<10;i++){
List<String> head = Lists.newArrayList();
head.add(i+"");
head.add(i*10+"");
head.add(i*10*3+"");
fyList.add(head);
}
//表头两列
for(int i =0 ; i<2;i++){
List<String> head = Lists.newArrayList();
head.add("表头"+i);
heads.add(head);
}
Map<String,Object> has = Maps.newHashMap();
try {
//动态生成表头与数据结果
EasyExcel.write(fileName).head(heads).sheet("模板2").doWrite(fyList);
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
if (excelWriter != null) {
excelWriter.finish();
}
}
}
结果展示
百万数据分解方案
每一百万分成一组list结果数据进行输出