当前位置:网站首页>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
边栏推荐
猜你喜欢
Nodejs安装及环境配置
基于微信小程序的wifi模块使用
Autumn recruitment in 2021, salary ranking No
PyMySQL
Un modèle universel pour la construction d'un modèle d'apprentissage scikit
基于ibeacons签到系统
Record a strange bug: component copy after cache component jump
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
基于CM管理的CDH集群集成Phoenix
Jenkins construction and use
随机推荐
生产环境——
org.apache.parquet.schema.InvalidSchemaException: A group type can not be empty. Parquet does not su
快捷键(多行)
JDBC入门
L2-024 tribe (25 points)
New关键字的学习和总结
Restful WebService和gSoap WebService的本质区别
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
Lin Lin, product manager of Lenovo: network failure of local network operator in Tianjin. The background server of Zui system can't work normally for the time being
Go语言 RPC通讯
pthread_self()为何重复了
There is a mining virus in the server
Prediction of tomorrow's trading limit of Low Frequency Quantization
smart-doc + torna生成接口文档
Node接入支付宝开放平台的沙箱实现支付功能
Expression「Func「TSource, object」」 转Expression「Func「TSource, object」」[]
Chapter I review of e-commerce spike products
关于pthread多线程一些好文章
Basic knowledge learning record
centOS下mysql主从配置