2021-06-18 poi-ooxml 4.1.0版本 Excel导入方法

本文介绍了一个基于Java的系统如何实现值班信息的Excel文件导入功能。通过使用ExcelImportUtil工具类,文章详细展示了Controller层与Service层的具体实现过程,包括读取文件、解析数据、验证用户信息及数据处理等关键步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Controller层

/**
 * 导入值班信息
 * @author LJC
 */
@ResponseBody
@RequestMapping("/importExcel")
public ResponseData importExcel(@RequestParam("dutyTaskId") Long dutyTaskId,@RequestParam("file") MultipartFile file){
    InputStream inputStream = null;
    try {
        inputStream = file.getInputStream();
        List<DutyInfoItem>  dutyInfoItemList = null;
        //声明导入参数封装对象
        ImportParams importParams = new ImportParams();
        dutyInfoItemList = ExcelImportUtil.importExcel(inputStream, DutyInfoItem.class,importParams);
        dutyInfoService.getimportExcel(dutyInfoItemList,dutyTaskId);
        return ResponseData.success();
    }catch (Exception e){
        e.printStackTrace();
        return ResponseData.error(e.getMessage());

    }
}

 Service层  为自己的逻辑代码 

 @Override
    public void getimportExcel(List<DutyInfoItem> dutyInfoItemList, Long dutyTaskId) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        //遍历excel中取到的数据
        AtomicInteger i = new AtomicInteger(1);
        dutyInfoItemList.forEach(node->{


            DutyInfoParam dutyInfoParam=new DutyInfoParam();
            BeanUtil.copyProperties(node,dutyInfoParam);
            //查找用户信息
            if (ToolUtil.isEmpty(node.getDate()) && ToolUtil.isEmpty(node.getPerName()) &&ToolUtil.isEmpty(node.getPostType()) && ToolUtil.isEmpty(node.getDeptName())){
                i.addAndGet(1);
                return;
            }
            User user = userMapper.getbyId(dutyInfoParam.getPerName());
            i.addAndGet(1);
            //判断用户是否存在
            if (ToolUtil.isEmpty(user)){
                throw new ServiceException(500,"表格第"+" "+ i +" "+"列存在错误,请修改!" );
            }

            String format = sdf.format(node.getDate());
            //判断用户是否已经添加过
            DutyInfoParam dip=dutyInfoMapper.getHisStroy(user.getUserId(), format);
            if (ToolUtil.isEmpty(dip)){
                //赋值用户Id
                dutyInfoParam.setPerId(user.getUserId());
                //赋值部门Id
                dutyInfoParam.setDeptId(user.getDeptId());
                //查找用户部门名称
                dutyInfoParam.setDeptName(deptService.getById(user.getDeptId()).getFullName());
                //赋值值班任务ID
                dutyInfoParam.setDutyTaskId(dutyTaskId);
                //添加用户
//                this.add(dutyInfoParam);
            }

        });
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值