EasyPoi 自定义数据处理

getRealKey()的作用是把源名替换为英文名,即学生姓名替换为name如此

MapImportHandler

java 复制代码
public class MapImportHandler extends ExcelDataHandlerDefaultImpl<Map<String, Object>> {

    @Override
    public void setMapValue(Map<String, Object> map, String originKey, Object value) {

       map.put(getRealKey(originKey), value != null ? getSexValue(value.toString()) : null);

    }

    private Object getSexValue(Object value){

        if (value.equals("男")){
            return "1";
        }

        if (value.equals("女")){
            return "0";
        }

        return value;
    }

    private String getRealKey(String originKey) {
        if (originKey.equals("学生姓名")) {
            return "name";
        }
        if (originKey.equals("学生性别")) {
            return "sex";
        }
        if (originKey.equals("进校日期")) {
            return "registrationDate";
        }
        return originKey;
    }
}

Controller层

java 复制代码
@CrossOrigin
    @PostMapping("/importStudentsToMapUseHandler")
    public Boolean importStudentsToMapUseHandler(@RequestParam("file") MultipartFile file) throws Exception {

        if (file.isEmpty()) {
            throw new Exception("error file is empty");
        }

        ImportParams params = new ImportParams();
        //设置title所占行数
        params.setTitleRows(1);
        params.setHeadRows(1);
        params.setDataHandler(new MapImportHandler());
        List<Map<String, Object>> studentsListMap = ExcelImportUtil.importExcel(file.getInputStream(), Map.class, params);

        List<Student> studentsList = new ArrayList<>();


        for (Map<String, Object> studentMap : studentsListMap) {

            String name = (String)studentMap.get("name");
            String sex = (String)studentMap.get("sex");
            String registrationDateStr = (String)studentMap.get("registrationDate");

            Student student = new Student();
            student.setName(name);

            student.setSex(Integer.parseInt(sex));


            LocalDate studengLocalDate = LocalDate.parse(registrationDateStr, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            LocalDateTime studentRegistrationLocalDateTime = LocalDateTime.of(studengLocalDate, LocalTime.MIDNIGHT);
            student.setRegistrationDate(studentRegistrationLocalDateTime);

            studentsList.add(student);

        }

        return studentService.saveBatch(studentsList);
    }
相关推荐
jgyzl1 小时前
2026.3.20 用EasyExcel实现excel报表的导入与导出
java·python·excel
Mr.45671 天前
Spring Boot 3 + EasyExcel 3.x 实战:构建高效、可靠的Excel导入导出服务
spring boot·后端·excel
如意机反光镜裸1 天前
excel怎么快速导入oracle
数据库·oracle·excel
开开心心就好1 天前
免费轻量级PDF阅读器,打开速度快
windows·计算机视觉·visualstudio·pdf·计算机外设·excel·myeclipse
城数派2 天前
2015-2025年我国区县逐年二手房房价数据(Excel/Shp格式)
excel
用户8356290780512 天前
Python 设置 Excel 条件格式教程
后端·python·excel
lzksword2 天前
关于EXCEL中vlookup身份证匹配失败的处理
excel
城数派2 天前
1990-2025年我国省市县三级的逐年土地覆盖数据(9类用地/Excel/Shp格式)
excel
catoop2 天前
构建高稳健性、可交互的复杂 Excel 报表方法论:切片、流式与动态公式
excel