poi导出多sheet excel
时间: 2023-08-24 08:07:06 浏览: 85
要导出多个sheet的Excel,可以使用Apache POI库。下面是一个简单的示例代码,它将数据导出到两个工作表(sheet)中:
```java
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
public class ExcelExporter {
public static void main(String[] args) throws IOException {
// create a new workbook
Workbook workbook = new XSSFWorkbook();
// create the first sheet
Sheet sheet1 = workbook.createSheet("Sheet1");
// create the first row
Row row1 = sheet1.createRow(0);
// create some cells
Cell cell1 = row1.createCell(0);
cell1.setCellValue("Hello");
Cell cell2 = row1.createCell(1);
cell2.setCellValue("World");
// create the second sheet
Sheet sheet2 = workbook.createSheet("Sheet2");
// create the first row
Row row2 = sheet2.createRow(0);
// create some cells
Cell cell3 = row2.createCell(0);
cell3.setCellValue("Foo");
Cell cell4 = row2.createCell(1);
cell4.setCellValue("Bar");
// write the workbook to a file
FileOutputStream fileOut = new FileOutputStream("workbook.xlsx");
workbook.write(fileOut);
fileOut.close();
// close the workbook
workbook.close();
}
}
```
在上面的代码中,我们创建了一个 `XSSFWorkbook` 对象,它表示一个Excel文件。然后,我们创建了两个工作表(sheet),分别命名为 "Sheet1" 和 "Sheet2"。接下来,我们向每个工作表中添加一些数据,并将工作簿(workbook)写入一个文件中。最后,我们关闭了工作簿(workbook)。
这是一个简单的示例,你可以根据自己的需要修改它。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)