- 导入依赖包
<!-- poi-->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.0.1</version>
</dependency>
- 获取Workbook
InputStream inputStream = new FileInputStream(filepath);
String sub = filepath.substring(filepath.lastIndexOf("."));
Workbook workbook = null;
if(sub.equals(".xls")){
workbook = new HSSFWorkbook(inputStream);
}
else if(sub.equals(".xlsx")){
workbook = new XSSFWorkbook(inputStream);
}
- 读取行列
Sheet sheet = workbook.getSheetAt(0);
int rows = sheet.getLastRowNum();
for(int i = 0;i<rows;i++){
Row row = sheet.getRow(i);
String str0 = row.getCell(0).toString();
System.out.println(str0);
}