poi 导入导出
时间: 2023-07-24 13:11:32 浏览: 69
Poi是Java语言的Excel文档处理组件,可以方便地读取、写入和操作Excel文档。Poi支持多种格式的Excel文件,如.xls和.xlsx等。下面是Poi的导入和导出示例代码:
1. 导入Excel文档
```java
FileInputStream fileInputStream = new FileInputStream(new File("file.xls"));
Workbook workbook = new HSSFWorkbook(fileInputStream); // HSSFWorkbook支持.xls格式
Sheet sheet = workbook.getSheetAt(0); // 获取第一个Sheet
Iterator<Row> iterator = sheet.iterator();
while (iterator.hasNext()) {
Row row = iterator.next();
Iterator<Cell> cellIterator = row.cellIterator();
while (cellIterator.hasNext()) {
Cell cell = cellIterator.next();
System.out.print(cell.toString() + ",");
}
System.out.println();
}
```
2. 导出Excel文档
```java
Workbook workbook = new HSSFWorkbook(); // HSSFWorkbook支持.xls格式
Sheet sheet = workbook.createSheet("Sheet1");
Row row = sheet.createRow(0);
Cell cell = row.createCell(0);
cell.setCellValue("Hello");
cell = row.createCell(1);
cell.setCellValue("World");
FileOutputStream fileOutputStream = new FileOutputStream(new File("file.xls"));
workbook.write(fileOutputStream);
fileOutputStream.close();
```
以上代码仅供参考,具体的应用需要根据实际情况进行修改。
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)