java ExcelWriterBuilder写入多sheet
时间: 2023-08-06 10:07:48 浏览: 171
要使用Java ExcelWriterBuilder写入多个sheet,可以按照以下步骤进行操作:
1. 创建Workbook对象
```java
Workbook workbook = new XSSFWorkbook();
```
2. 创建多个Sheet对象,并指定名称
```java
Sheet sheet1 = workbook.createSheet("Sheet1");
Sheet sheet2 = workbook.createSheet("Sheet2");
Sheet sheet3 = workbook.createSheet("Sheet3");
```
3. 分别写入数据到不同的Sheet对象
```java
// 写入数据到Sheet1
for (int rowNum = 0; rowNum < 10; rowNum++) {
Row row = sheet1.createRow(rowNum);
Cell cell1 = row.createCell(0);
cell1.setCellValue("Row " + (rowNum + 1));
Cell cell2 = row.createCell(1);
cell2.setCellValue("Data");
}
// 写入数据到Sheet2
for (int rowNum = 0; rowNum < 5; rowNum++) {
Row row = sheet2.createRow(rowNum);
Cell cell1 = row.createCell(0);
cell1.setCellValue("Row " + (rowNum + 1));
Cell cell2 = row.createCell(1);
cell2.setCellValue("Data");
}
// 写入数据到Sheet3
for (int colNum = 0; colNum < 10; colNum++) {
Row row = sheet3.createRow(0);
Cell cell = row.createCell(colNum);
cell.setCellValue("Column " + (colNum + 1));
for (int rowNum = 1; rowNum < 6; rowNum++) {
row = sheet3.createRow(rowNum);
cell = row.createCell(colNum);
cell.setCellValue("Row " + rowNum + ", Col " + (colNum + 1));
}
}
```
4. 使用FileOutputStream将Workbook对象写入Excel文件
```java
FileOutputStream fileOut = new FileOutputStream("output.xlsx");
workbook.write(fileOut);
fileOut.close();
```
这样就可以使用Java ExcelWriterBuilder写入多个sheet了。记得在写入数据之前先创建好Sheet对象,并指定名称。
阅读全文