当前位置:网站首页>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
边栏推荐
猜你喜欢
Intégration de Clusters CDH Phoenix basée sur la gestion cm
Quartus prime hardware experimental development (de2-115 board) experiment 1 CPU instruction calculator design
Crontab timing task output generates a large number of mail and runs out of file system inode problem processing
Special test 05 · double integral [Li Yanfang's whole class]
visio安装报错 1:1935 2:{XXXXXXXX...
Cdh6 based on CM management 3.2 cluster integration atlas 2 one
Strange bug of cnpm
Multithreading
基于Ocelot的gRpc网关
PySide2
随机推荐
Record a strange bug: component copy after cache component jump
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
Pytorch 经典卷积神经网络 LeNet
SQL: How to parse Microsoft Transact-SQL Statements in C# and to match the column aliases of a view
Windos中安装labellmg教程
PyMySQL
Go语言 RPC通讯
DDT+Excel进行接口测试
L2-024 tribe (25 points)
微信小程序调用客服接口
关于pthread多线程一些好文章
基础知识学习记录
对List集合进行分页
linux MySQL数据定时dump
基于CM管理的CDH6.3.2集群集成Atlas2.1.0
Prediction of tomorrow's trading limit of Low Frequency Quantization
修改ddt生成的测试用例名称
理解虚基类、虚函数与纯虚函数的概念(转)
Strange bug of cnpm
JDBC入门