EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。
官网:https://www.yuque.com/easyexcel/doc/easyexcel
1.maven导入
com.alibaba easyexcel 3.0.5
2.创建实体类
注解中的名称应与excel表头的名称一致
excel表
3.创建实体类
@Data
public class WeatherExcelData {
@ExcelProperty("Location_ID")
private String Location_ID;
@ExcelProperty("Location_Name_ZH")
private String locationName;
@ExcelProperty("Adm1_Name_ZH")
private String primaryArea;
@ExcelProperty("Adm2_Name_ZH")
private String SecondArea;
}
4.使用easyexcel读取excel文件后,存入缓存。使用Collator类,重写compare比较方法。使用stream流根据一级地区名,二级地区名分组排序。
此处使用easyexcel最简单的读,更多使用方式可查询官网
public class ResourceController {
private static Map locationMap = new HashMap<>();
private static List locationList = new ArrayList<>();
private static TreeMap>> treeMap = new TreeMap();
public void getExcel(){
try{
// excel文件放于resources文件夹下,通过ClassPathResource形式读取,打包后放置linux环境下也可正常读取数据
ClassPathResource resource = new ClassPathResource("China-City-List-latest.xlsx");
// 这里 需要指定读用哪个class去读,然后读取第一个sheet 文件流会自动关闭
// 这里每次会读取3000条数据 然后返回过来 直接调用使用数据就行
EasyExcel.read(resource.getInputStream(), WeatherExcelData.class, new PageReadListener(dataList -> {
for (WeatherExcelData data : dataList) {
locationMap.put(data.getLocationName() + "", data);
}
locationList.addAll(dataList);
})).sheet().doRead();
putLocationCache(locationList);
}catch (Exception e){
log.error("读取地区文件错误");
}
}
public void putLocationCache(List locationList) {
if(locationList.size() == 0){
return;
}
TreeMap>> newLocation = new TreeMap<>(new Comparator 


