请写出java使用EasyExcel导出多个sheet的代码
时间: 2024-05-08 13:15:23 浏览: 97
以下是使用EasyExcel导出多个sheet的示例代码:
```
//创建ExcelWriter对象
ExcelWriter writer = EasyExcel.write(filePath).build();
//定义sheet1数据
List<DemoData1> sheet1Data = new ArrayList<>();
sheet1Data.add(new DemoData1("张三", 20));
sheet1Data.add(new DemoData1("李四", 25));
sheet1Data.add(new DemoData1("王五", 30));
//定义sheet2数据
List<DemoData2> sheet2Data = new ArrayList<>();
sheet2Data.add(new DemoData2("A", "B"));
sheet2Data.add(new DemoData2("C", "D"));
sheet2Data.add(new DemoData2("E", "F"));
//写入sheet1
writer.write(sheet1Data, EasyExcel.writerSheet(0, "sheet1").head(DemoData1.class).build());
//写入sheet2
writer.write(sheet2Data, EasyExcel.writerSheet(1, "sheet2").head(DemoData2.class).build());
//关闭ExcelWriter对象
writer.finish();
```
其中,DemoData1和DemoData2是两个自定义的数据类,分别用于存储sheet1和sheet2的数据。在上面的代码中,我们先创建了一个ExcelWriter对象,然后分别定义了sheet1和sheet2的数据,并通过EasyExcel.writerSheet()方法来创建对应的sheet,并传入数据和表头信息。最后,我们调用writer.finish()方法关闭ExcelWriter对象,完成导出操作。
阅读全文