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);
    }
相关推荐
Access开发易登软件2 天前
数据处理中的两大基石:何时选择Excel,何时考虑Access
数据库·信息可视化·excel·vba·access
开开心心就好2 天前
PDF密码移除工具,免费解除打印编辑复制权限
java·网络·windows·websocket·pdf·电脑·excel
weixin_462446232 天前
Python 实战:将 HTML 表格一键导出为 Excel(xlsx)
linux·python·excel·pandas
Data-Miner2 天前
Excel-Agent永久买断,一款结合AI Agent的excel数据处理智能体
人工智能·excel
weixin_462446232 天前
Python 使用 openpyxl 从 URL 读取 Excel 并获取 Sheet 及单元格样式信息
python·excel·openpyxl
wtsolutions2 天前
MCP Server Integration - JSON to Excel for AI and Automation
json·excel
开开心心就好2 天前
音频编辑工具,多端支持基础剪辑易操作
java·网络·windows·java-ee·电脑·maven·excel
vfvfb2 天前
excel多个合并 xlsx工作表合并 多个excel合并到一张表
excel
金融小白数据分析之路3 天前
msoffcrypto-tool库 Excel 加密
python·excel
开开心心_Every3 天前
文件数量统计工具:支持多层文件夹数量统计
游戏·微信·pdf·excel·语音识别·swift·lisp