1、pom.xml配置文件
1 <!-- 配置Apache POI --> 2 <dependency> 3 <groupId>org.apache.poi</groupId> 4 <artifactId>poi</artifactId> 5 <version>4.1.0</version> 6 </dependency> 7 <dependency> 8 <groupId>org.apache.poi</groupId> 9 <artifactId>poi-ooxml</artifactId> 10 <version>4.1.0</version> 11 </dependency> 12 <dependency> 13 <groupId>org.apache.poi</groupId> 14 <artifactId>poi-ooxml-schemas</artifactId> 15 <version>4.1.0</version> 16 </dependency>
2、读取Excel的Java代码
1 @Test 2 public void readExcelTest() throws IOException { 3 File xlsFile = new File("E:\book.xlsx"); 4 // 获得工作簿 5 XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(xlsFile)); 6 // 获得sheet 7 XSSFSheet sheet = wb.getSheetAt(0); 8 int rows = sheet.getPhysicalNumberOfRows(); 9 String split = "**********"; 10 for (int i = 0; i < rows; i++) { 11 // 获取第i行 12 XSSFRow row = sheet.getRow(i); 13 // 列数 14 short nums = row.getLastCellNum(); 15 for (int j = 0; j < nums; j++) { 16 Cell cell = row.getCell(j); 17 System.out.print(cell + split); 18 } 19 System.out.println(); 20 } 21 }
3、Excel文件和测试结果
以上只是java读取Excel应用演示,代码中没有对对象的非空判定,需要的童鞋可自行加上判定,使代码更健壮。