当前位置:网站首页>Easyexcel读取excel表地理位置数据,按中文拼音排序
Easyexcel读取excel表地理位置数据,按中文拼音排序
2022-04-23 14:03:00 【洋哥登陆】
使用Easyexcel读取excel文件
EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。
官网:https://www.yuque.com/easyexcel/doc/easyexcel
1.maven导入
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
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<String, WeatherExcelData> locationMap = new HashMap<>();
private static List<WeatherExcelData> locationList = new ArrayList<>();
private static TreeMap<String, Map<String, List<WeatherExcelData>>> 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<WeatherExcelData>(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<WeatherExcelData> locationList) {
if(locationList.size() == 0){
return;
}
TreeMap<String, Map<String, List<WeatherExcelData>>> newLocation = new TreeMap<>(new Comparator<Object>() {
// 设置Locale.CHINA按中文排序
Collator collator = Collator.getInstance(Locale.CHINA);
@Override
public int compare(Object object1, Object object2) {
if (object1 == null || object2 == null){
return 0;
}
CollationKey keyOne = collator.getCollationKey(String.valueOf(object1));
CollationKey keyTwo = collator.getCollationKey(String.valueOf(object2));
return keyOne.compareTo(keyTwo);
}
});
// 转化为TreeMap,先按一级地区名,再按二级地区名分组排序
Map<String, Map<String, List<WeatherExcelData>>> location = locationList.stream()
.collect(Collectors.groupingBy(WeatherExcelData::getPrimaryArea,
Collectors.groupingBy(WeatherExcelData::getSecondArea, Collectors.toList())));
for (Map.Entry<String, Map<String, List<WeatherExcelData>>> entry : location.entrySet()) {
newLocation.put(entry.getKey(), entry.getValue());
}
treeMap = newLocation;
System.out.println(treeMap);
}
}
版权声明
本文为[洋哥登陆]所创,转载请带上原文链接,感谢
https://blog.csdn.net/Peanutfight/article/details/124332414
边栏推荐
- Analysis and understanding of atomicintegerarray source code
- 微信小程序进行蓝牙初始化、搜索附近蓝牙设备及连接指定蓝牙(一)
- Choreographer全解析
- Android: answers to the recruitment and interview of intermediate Android Development Agency in early 2019 (medium)
- FDFS start
- 关于stream流,浅记一下------
- Neuron and neural network
- Node接入支付宝开放平台的沙箱实现支付功能
- 微信小程序的订阅号开发(消息推送)
- There is a mining virus in the server
猜你喜欢
基于微信小程序的wifi模块使用
CentOS mysql多实例部署
Prediction of tomorrow's trading limit of Low Frequency Quantization
微信小程序调用客服接口
How does redis solve the problems of cache avalanche, cache breakdown and cache penetration
Autumn recruitment in 2021, salary ranking No
DDT+Excel进行接口测试
1256: bouquet for algenon
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
STM32学习记录0007——新建工程(基于寄存器版)
随机推荐
蓝绿发布、滚动发布、灰度发布,有什么区别?
分库分表 & ShardingSphere
JMeter pressure test tool
Business case | how to promote the activity of sports and health app users? It is enough to do these points well
多线程 @Async 线程池
收藏博客贴
Kettle -- control parsing
Chapter 15 new technologies of software engineering
基于微信小程序的wifi模块使用
项目中遇到的问题(五)操作Excel接口Poi的理解
Port occupied 1
As a junior college student, I studied hard in closed doors for 56 days, won Ali offer with tears, five rounds of interviews and six hours of soul torture
BUG_me
DDT+Excel进行接口测试
PyMySQL
Android篇:2019初中级Android开发社招面试解答(中
接口文档yaml
CentOS mysql多实例部署
centOS下mysql主从配置
Un modèle universel pour la construction d'un modèle d'apprentissage scikit