easyexcel如何组装多个sheet
时间: 2023-06-15 07:02:55 浏览: 137
在easyexcel中,可以通过创建多个Sheet对象并将它们添加到同一个Workbook对象中来组装多个sheet。具体步骤如下:
1. 创建Workbook对象:使用EasyExcel工具类的`WorkbookFactory.create()`方法创建一个Workbook对象。
2. 创建Sheet对象:使用EasyExcel工具类的`Sheet(sheetNo, head)`方法创建一个Sheet对象,其中sheetNo为sheet的序号,head为表头信息。
3. 将Sheet对象添加到Workbook对象中:使用Workbook对象的`addSheet()`方法将Sheet对象添加到Workbook对象中,可以重复执行此步骤添加多个Sheet对象。
4. 写入数据:使用EasyExcel工具类的`write()`方法向Sheet对象中写入数据,可以重复执行此步骤向多个Sheet对象中写入数据。
5. 输出文件:使用EasyExcel工具类的`write()`方法将Workbook对象输出为Excel文件。
下面是一个示例代码,展示如何组装多个sheet:
```java
// 创建Workbook对象
Workbook workbook = WorkbookFactory.create(true);
// 创建Sheet1对象
Sheet sheet1 = new Sheet(1, 0, DemoData.class, "第一个sheet", null);
// 将Sheet1对象添加到Workbook对象中
workbook.addSheet(sheet1);
// 向Sheet1对象中写入数据
List<DemoData> data1 = getData1();
EasyExcel.write(sheet1).head(DemoData.class).sheet().doWrite(data1);
// 创建Sheet2对象
Sheet sheet2 = new Sheet(2, 0, DemoData.class, "第二个sheet", null);
// 将Sheet2对象添加到Workbook对象中
workbook.addSheet(sheet2);
// 向Sheet2对象中写入数据
List<DemoData> data2 = getData2();
EasyExcel.write(sheet2).head(DemoData.class).sheet().doWrite(data2);
// 输出文件
OutputStream out = new FileOutputStream("multi_sheet.xlsx");
workbook.write(out);
out.close();
```
其中,`DemoData`为数据实体类,`getData1()`和`getData2()`方法分别返回两个sheet需要写入的数据列表。
阅读全文
相关推荐
















