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);
    }
相关推荐
ohoy6 小时前
easypoi 自定义样式 学生-分数红绿颜色设置
excel
ranchor6667 小时前
excel+pandas使用str.contains() 的典型例子
excel·pandas
Neoest7 小时前
【Java 填坑日记】Excel里的“1.00“存入数据库解密后,Integer说它不认识:一次 NumberFormatException 翻车实录
java·数据库·excel
lzq6037 小时前
Python自动化办公:5分钟批量处理Excel数据
python·自动化·excel
oh,huoyuyan7 小时前
【实战案例】使用火语言RPA『表格数据提取』组件,批量爬取蔬菜价格+Excel 整理
爬虫·excel·rpa
缺点内向7 小时前
如何在Excel文档中获取分页信息
后端·c#·.net·excel
黑客思维者7 小时前
Python自动化办公全攻略:Excel/Word/PDF/邮件批量处理
python·自动化·excel
caleb_5201 天前
Excel导出问题:accessExternalStylesheet
excel
YANshangqian1 天前
图片转Excel表格
excel