- 导入依赖
<!-- 解析excel -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>3.13</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>3.13</version>
</dependency>
解析代码
private ArrayList<CityCode> getCityCodes() throws IOException {
//获取项目地址
// String ss = System.getProperty("user.dir");
//excel 地址
// String url = ss+"\\src\\main\\resources\\static\\excel\\AMap_adcode_citycode.xlsx";
//打war包读取资源文件
ClassPathResource resource = new ClassPathResource("static/excel/AMap_adcode_citycode.xlsx");
InputStream inputStream = resource.getInputStream();
//获取 输入流
FileInputStream inputStream=new FileInputStream(new File(url));
XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
//sheet 包含excel内容
XSSFSheet sheet = workbook.getSheetAt(0);
//getLastRowNum 总行数
ArrayList<CityCode> list=new ArrayList<CityCode>();
for(int i=2 ;i<sheet.getLastRowNum();i++){
CityCode cityCode=new CityCode();
XSSFRow xssfRow = sheet.getRow(i);
//获取行的第一列内容
String city =xssfRow.getCell(0).getStringCellValue();
//获取行的第二zuih列内容
String code =xssfRow.getCell(1).getStringCellValue();
cityCode.setAdcode(code);
cityCode.setCity(city);
list.add(cityCode);
}
return list;
}