POI 读取Excel数据

时间:2024-06-07 18:34:20
private List<LeagueGroup> read() throws IOException{
List<LeagueGroup> leagueGroups=new ArrayList<LeagueGroup>();
String filePath="C:\\Users\\Louis\\AppData\\Roaming\\Skype\\My Skype Received Files\\LeagueGroup2.xlsx";
InputStream is = new FileInputStream(filePath);
@SuppressWarnings("resource")
XSSFWorkbook xwb = new XSSFWorkbook(is);
XSSFSheet sheet = xwb.getSheetAt(0);
XSSFRow row;
for (int i = sheet.getFirstRowNum(); i < sheet.getPhysicalNumberOfRows(); i++) {
row = sheet.getRow(i);
LeagueGroup leagueGroup=new LeagueGroup();
leagueGroup.setLeagueGroupName(row.getCell(0).toString());
leagueGroup.setLeagueGroupId(Long.parseLong(row.getCell(1).toString()));
leagueGroup.setSportId(new Double(row.getCell(2).getNumericCellValue()).longValue());
leagueGroups.add(leagueGroup);
System.out.println(i+" - "+leagueGroup.getLeagueGroupId()+" - "+leagueGroup.getLeagueGroupName()+" - "+leagueGroup.getSportId());
}
return leagueGroups;
}
     <dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.16</version>
</dependency>