easyexcel导出excel,如何多sheet
时间: 2023-11-02 10:00:43 浏览: 142
要在 EasyExcel 中导出多个 sheet,可以使用 `Sheet` 对象。首先创建一个 `ExcelWriter` 对象,然后使用 `ExcelWriter` 对象创建多个 `Sheet` 对象,最后将数据写入到每个 `Sheet` 中。
以下是导出多个 sheet 的示例代码:
```java
// 创建 ExcelWriter 对象
ExcelWriter writer = EasyExcel.write(fileName).build();
// 创建 Sheet1 对象并写入数据
Sheet sheet1 = new Sheet(1, 0, Model1.class);
sheet1.setSheetName("Sheet1");
List<Model1> model1List = getDataForSheet1();
writer.write(model1List, sheet1);
// 创建 Sheet2 对象并写入数据
Sheet sheet2 = new Sheet(2, 0, Model2.class);
sheet2.setSheetName("Sheet2");
List<Model2> model2List = getDataForSheet2();
writer.write(model2List, sheet2);
// 关闭 ExcelWriter
writer.finish();
```
在上述示例代码中,`ExcelWriter` 对象用于创建和管理 Excel 文件,`Sheet` 对象用于定义每个 sheet 的名称、数据类型和数据行数等信息。`write()` 方法用于将数据写入到指定的 sheet 中,`finish()` 方法用于关闭 `ExcelWriter` 对象并将数据写入到磁盘文件中。
注意:`Sheet` 对象中的 `sheetNo` 参数表示 sheet 的编号,从 1 开始计数;`headRowNumber` 参数表示表头所在行数,从 0 开始计数。
阅读全文
相关推荐















