栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Easyexcel读取excel表地理位置数据,按中文拼音排序

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Easyexcel读取excel表地理位置数据,按中文拼音排序

使用Easyexcel读取excel文件

EasyExcel是一个基于Java的简单、省内存的读写Excel的开源项目。
官网:https://www.yuque.com/easyexcel/doc/easyexcel

1.maven导入


   com.alibaba
   easyexcel
   3.0.5

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 locationMap = new HashMap<>();
    private static List locationList = new ArrayList<>();
    private static TreeMap>> 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(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 locationList) {
        if(locationList.size() == 0){
            return;
        }

        TreeMap>> newLocation = new TreeMap<>(new Comparator() {
        	// 设置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>> location = locationList.stream()
                .collect(Collectors.groupingBy(WeatherExcelData::getPrimaryArea,
                        Collectors.groupingBy(WeatherExcelData::getSecondArea, Collectors.toList())));
        for (Map.Entry>> entry : location.entrySet()) {
            newLocation.put(entry.getKey(), entry.getValue());
        }

        treeMap = newLocation;
        System.out.println(treeMap);
    }
}


转载请注明:文章转载自 www.mshxw.com
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号