java - 操作Excel文件

<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.14</version>
</dependency>

<!-- ooxml -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.14</version>
</dependency>

Workbook workbook = WorkbookFactory.create(file.getInputStream());可以兼容xls 和xlsx 文件
Sheet sheetAt = workbook.getSheetAt(0);  获取sheet 索引为0的
Row row = sheetAt.getRow(0);  获取第几行
int row = sheetAt.getLastRowNum();  获取最后一行的索引
Row row = sheetAt.createRow(1);  创建不存在的行索引  、、 默认为空的单元格都需要创建
Cell cell = row.createCell(1);  创建行的第几个单元格  
Cell cell0 = row.getCell(0);  获取行的第几个单元格 就是单元格值
cell.setCellValue("h2");  设置单元格值

写入文件时,先打开file;
workbook.write(fileOutputStream);  写入excel 文件

打开流文件,及时关闭

猜你喜欢

转载自www.cnblogs.com/pengranxindong/p/10193215.html