当前位置:网站首页>Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
Easyexcel reads the geographical location data in the excel table and sorts them according to Chinese pinyin
2022-04-23 16:35:00 【Yangge landing】
Use Easyexcel Read excel file
EasyExcel It's based on Java Simple 、 Save memory for reading and writing Excel Open source projects for .
Official website :https://www.yuque.com/easyexcel/doc/easyexcel
1.maven Import
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>3.0.5</version>
</dependency>
2. Create entity class
The name in the note should be the same as excel The name of the header is consistent
excel surface
3. Create entity class
@Data
public class WeatherExcelData {
@ExcelProperty("Location_ID")
private String Location_ID;
/** * Region name */
@ExcelProperty("Location_Name_ZH")
private String locationName;
/** * First class area name */
@ExcelProperty("Adm1_Name_ZH")
private String primaryArea;
/** * Name of secondary area */
@ExcelProperty("Adm2_Name_ZH")
private String SecondArea;
}
4. Use easyexcel Read excel After the document , Deposited in the cache . Use Collator class , rewrite compare Comparison method . Use stream The stream is named according to the first level region , Second level region name grouping sorting .
Use here easyexcel The simplest reading , For more usage, please check the official website
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 The document is placed in resources Under the folder , adopt ClassPathResource Form read , Place after packaging linux Data can also be read normally in the environment
ClassPathResource resource = new ClassPathResource("China-City-List-latest.xlsx");
// here You need to specify which to read class To read , Then read the first sheet The file stream closes automatically
// It reads every time 3000 Data Then come back Just call and use the data directly
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(" Error reading region file ");
}
}
/** * Cache geographic location */
public void putLocationCache(List<WeatherExcelData> locationList) {
if(locationList.size() == 0){
return;
}
TreeMap<String, Map<String, List<WeatherExcelData>>> newLocation = new TreeMap<>(new Comparator<Object>() {
// Set up Locale.CHINA Sort by Chinese
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);
}
});
// Turn into TreeMap, First, press the first-class region name , Then sort by the name of the secondary region
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);
}
}
版权声明
本文为[Yangge landing]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231402542649.html
边栏推荐
- Detailed explanation of gzip and gunzip decompression parameters
- Hypermotion cloud migration completes Alibaba cloud proprietary cloud product ecological integration certification
- Win11 / 10 home edition disables the edge's private browsing function
- Force buckle-746 Climb stairs with minimum cost
- logback的配置文件加载顺序
- G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
- Solution of garbled code on idea console
- Install redis and deploy redis high availability cluster
- How to upgrade openstack across versions
- DanceNN:字节自研千亿级规模文件元数据存储系统概述
猜你喜欢
Sort by character occurrence frequency 451
Homewbrew installation, common commands and installation path
NVIDIA显卡驱动报错
How magical is the unsafe class used by all major frameworks?
Nacos 详解,有点东西
Real time operation of vim editor
Summary according to classification in sail software
Solution of garbled code on idea console
下载并安装MongoDB
You need to know about cloud disaster recovery
随机推荐
Real time operation of vim editor
Day 9 static abstract class interface
File system read and write performance test practice
Cartoon: what are IAAs, PAAS, SaaS?
阿里研发三面,面试官一套组合拳让我当场懵逼
G008-hwy-cc-estor-04 Huawei Dorado V6 storage simulator configuration
ESXi封装网卡驱动
Day (6) of picking up matlab
Gartner 發布新興技術研究:深入洞悉元宇宙
Meaning and usage of volatile
Gartner 发布新兴技术研究:深入洞悉元宇宙
Nacos 详解,有点东西
LVM and disk quota
磁盘管理与文件系统
vim编辑器的实时操作
面试题 17.10. 主要元素
Use if else to judge in sail software - use the title condition to judge
Postman batch production body information (realize batch modification of data)
最详细的背包问题!!!
How to quickly batch create text documents?