当前位置:网站首页>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
边栏推荐
- Use if else to judge in sail software - use the title condition to judge
- 力扣-198.打家劫舍
- MySql主从复制
- logback的配置文件加载顺序
- The system research problem that has plagued for many years has automatic collection tools, which are open source and free
- JSP learning 1
- How magical is the unsafe class used by all major frameworks?
- G008-HWY-CC-ESTOR-04 华为 Dorado V6 存储仿真器配置
- Gartner predicts that the scale of cloud migration will increase significantly; What are the advantages of cloud migration?
- Set the color change of interlaced lines in cells in the sail software and the font becomes larger and red when the number is greater than 100
猜你喜欢

Postman batch production body information (realize batch modification of data)

Meaning and usage of volatile

You need to know about cloud disaster recovery

Cloudy data flow? Disaster recovery on cloud? Last value content sharing years ago

Countdown 1 day ~ 2022 online conference of cloud disaster tolerance products is about to begin

Gartner 发布新兴技术研究:深入洞悉元宇宙

What is the experience of using prophet, an open source research tool?

Query the data from 2013 to 2021, and only query the data from 2020. The solution to this problem is carried out

OMNeT学习之新建工程

Day 9 static abstract class interface
随机推荐
Hypermotion cloud migration helped China Unicom. Qingyun completed the cloud project of a central enterprise and accelerated the cloud process of the group's core business system
The solution of not displaying a whole line when the total value needs to be set to 0 in sail software
ESXi封装网卡驱动
如何进行应用安全测试(AST)
Install redis and deploy redis high availability cluster
Use if else to judge in sail software - use the title condition to judge
Use itextpdf to intercept the page to page of PDF document and divide it into pieces
Passing header request header information between services through feign
Introduction notes to PHP zero Foundation (13): array related functions
力扣-198.打家劫舍
Day 10 abnormal mechanism
How to quickly batch create text documents?
阿里研发三面,面试官一套组合拳让我当场懵逼
安装Redis并部署Redis高可用集群
Start Oracle service on Linux
ESP32_ Arduino
【Pygame小游戏】10年前风靡全球的手游《愤怒的小鸟》,是如何霸榜的?经典回归......
LVM and disk quota
Gartner 發布新興技術研究:深入洞悉元宇宙
磁盘管理与文件系统