java 中使用easypoi 导入excel

 maven依赖

        <!--easypoi与SpringBoot的整合包-->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.2</version>
        </dependency>

 下面是具体实现方法

public AjaxResult importData(MultipartFile file, String seriesCode) {
        try { 
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        params.setNeedVerify(true);
        //自定义校验
        params.setVerifyHandler(importVerify);
        params.setVerifyFileSplit(false);
        List<TGatherAccountingFolder> success = null;
        Map<String, Object> res = new HashMap<>();
        List<String> errMsgList = new ArrayList<>();

        long millis = System.currentTimeMillis();
			//ExcelImportUtil.importExcelMore 直接调用 
            ExcelImportResult<TGatherAccountingFolder> result = ExcelImportUtil.importExcelMore(file.getInputStream(), TGatherAccountingFolder.class, params);
            success = result.getList();
            //失败结果集
            List<TGatherAccountingFolder> failList = result.getFailList();


            //验证是否有失败的数据
            if (result.isVerifyFail()) {
                for (TGatherAccountingFolder accountFolder : failList) {
                    errMsgList.add("第" + accountFolder.getRowNum() + "条 ," + accountFolder.getErrorMsg());
                }
                res.put("errMsg", errMsgList);
                res.put("msg", "失败" + failList.size() + "条");
                return AjaxResult.error("导入失败", res);
            } else {
                //导入序号和册号规则
                if (success.get(0).getfCeh() != 1) {
                    errMsgList.add("第一条记录册号填写错误");
                }
                for (int i = 0; i < success.size(); i++) {
                    if (success.get(i).getfSort() - i != 1) {
                        errMsgList.add("序号不连续");
                    }
                    for (int j = i + 1; j < success.size(); j++) {
                        int ce = success.get(j).getfCeh() - success.get(i).getfCeh();
                        if (success.get(j).getfCeh() > success.get(i).getfCeh() && ce != 1) {
                            errMsgList.add("第" + success.get(j).getfSort() + "条记录册号填写错误");
                        }
                        break;
                    }
                }
                if (null != errMsgList && errMsgList.size() > 0) {
                    res.put("errMsg", errMsgList);
                    return AjaxResult.error("导入失败", res);
                }

                int i = DateUtil.thisYear();
                for (TGatherAccountingFolder accountFolder : success) {
                    //导入成功的数据 进行持久化操作
                }
                super.saveBatch(success);
                //操作日志
                

                return AjaxResult.success(success);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.success();
    }

其中 params.setVerifyHandler(importVerify); 为添加自定义校验方法,对实体中单个对象的业务逻辑进行校验:

@Component
public class AccountingImportVerify implements IExcelVerifyHandler<TGatherAccountingFolder> {

    @Resource
    private ITGatherAccountingFolderService folderService;
    @Resource
    private DepositVerify depositVerify;
    @Resource
    private ITUseSimpleQueryAccountingService accountingService;


    @Override
    public ExcelVerifyHandlerResult verifyHandler(TGatherAccountingFolder accountingFolder) {
        //设置默认验证为true
        ExcelVerifyHandlerResult excelVerifyHandlerResult = new ExcelVerifyHandlerResult(true);


        //归档年度
        int thisYear = DateUtil.thisYear();
        if (null != accountingFolder.getfYearCode()){
            if (!Pattern.matches(GatherConstant.REGEX_YEAR, accountingFolder.getfYearCode().toString()) || accountingFolder.getfYearCode() > thisYear){
                excelVerifyHandlerResult.setSuccess(false);
                excelVerifyHandlerResult.setMsg(StringUtils.isNotEmpty(excelVerifyHandlerResult.getMsg())?excelVerifyHandlerResult.getMsg()+"形成年度格式不正确," : "" +"形成年度格式不正确,");
            }
        }

        //条形码
        if (StringUtils.isNotEmpty(accountingFolder.getfBarcodeNo())){
            List<TGatherAccountingFolder> barCode = folderService.list(new QueryWrapper<TGatherAccountingFolder>().lambda()
                    .eq(TGatherAccountingFolder::getfBarcodeNo, accountingFolder.getfBarcodeNo()));
            if (null != barCode && barCode.size() > 0){
                excelVerifyHandlerResult.setSuccess(false);
                excelVerifyHandlerResult.setMsg(StringUtils.isNotEmpty(excelVerifyHandlerResult.getMsg()) ? excelVerifyHandlerResult.getMsg()+ "条形码重复!": "条形码重复!");
            }
            List<TUseSimpleQueryAccountingFolderEntity> barCodeCore = accountingService.list(new QueryWrapper<TUseSimpleQueryAccountingFolderEntity>().lambda()
                    .eq(TUseSimpleQueryAccountingFolderEntity::getfBarcodeNo, accountingFolder.getfBarcodeNo()));
            if (null != barCodeCore && barCodeCore.size() > 0){
                excelVerifyHandlerResult.setSuccess(false);
                excelVerifyHandlerResult.setMsg(StringUtils.isNotEmpty(excelVerifyHandlerResult.getMsg()) ? excelVerifyHandlerResult.getMsg()+ "条形码重复!": "条形码重复!");
            }
        }



        return excelVerifyHandlerResult;
    }
}

实体对象中可添加easypoi的  @Excel 即可:

 具体可查看EasyPoi官网

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沉浮yu大海

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值