Easypoi一对多实现导入

使用 Easypoi一对多数据 实现导入 实质添加数据入库

最近在项目上实现了一对多的数据导入,需求导入的模板如下:
在这里插入图片描述
解决方案:

1.导入依赖 这里就不详细介绍了 网上特别多
2.创建可以完成此标题的实体类

2.1 创建 CertificateConfigCluster 实体类

@Data
public class CertificateConfigCluster {

    /** 
     *  @Excel(needMerge = true) 是否要合并单元格
     * @Excel(importFormat = "yyyy-MM-dd") importFormat 导入时候处理xls表格的日期格式
     * @Excel 注解中 还有很多的属性 比如字段判空 等等 大家可以上官网自行查看
     */

    /**
     * @Fields certificateName 证书名称
     */
    @Excel(needMerge = true, name = "证书名称", width = 20)
    private String certificateName;
    /**
     * @Fields certificateExpiryTime 证书到期时间
     */
    @Excel(needMerge = true, name = "到期时间", width = 20, importFormat = "yyyy-MM-dd")
    private LocalDateTime certificateExpiryTime;
    /**
     * 子集
     */
    @ExcelCollection(name = "应用信息")
    private List<CertificateApplicationConfigCluster> certificateApplicationConfigClusterList;
}

2.2 创建 一对多实体类 CertificateApplicationConfigCluster

@Data
public class CertificateApplicationConfigCluster implements IExcelDataModel {
    /**
     * @Fields applicationName 应用名称
     */
    @Excel(name = "部署应用", width = 20)
    private String applicationName;
    /**
     * @Fields businessContact 业务联系人
     */
    @Excel(name = "业务联系人", width = 20)
    private String businessContact;
    /**
     * @Fields businessMobileNumber 手机号码(业务)
     */
    @Excel(name = "手机号码(业务)", width = 20)
    private String businessMobileNumber;
    /**
     * @Fields businessMailbox 邮箱(业务)
     */
    @Excel(name = "邮箱(业务)", width = 20)
    private String businessMailbox;
    /**
     * @Fields technicalContact 技术联系人
     */
    @Excel(name = "技术联系人", width = 20)
    private String technicalContact;
    /**
     * @Fields technicalPhoneNumber 手机号(技术)
     */
    @Excel(name = "手机号码(技术)", width = 20)
    private String technicalPhoneNumber;
    /**
     * @Fields technicalMailbox 邮箱(技术)
     */
    @Excel(name = "邮箱(技术)", width = 20)
    private String technicalMailbox;

    /**
     * 行数 注意: 使用了 lombok 中 @data注解 可以不重写 下面两个方法 没有影响
     */
    private Integer rowNum;

    /**
     * 获取行数
     * @return
     */
    @Override
    public Integer getRowNum() {
        return rowNum;
    }

    /**
     * 设置行数
     * @param rowNum
     */
    @Override
    public void setRowNum(Integer rowNum) {
        this.rowNum = rowNum;
    }
}

实现 IExcelDataModel 接口可以获取行数 附源码

/**
 * Excel 本身数据文件
 * @author by jueyue on 18-4-8.
 */
public interface IExcelDataModel {

    /**
     * 获取行号
     * @return
     */
    public Integer getRowNum();

    /**
     *  设置行号
     * @param rowNum
     */
    public void setRowNum(Integer rowNum);

}

实现 IExcelModel接口可以获取错误数据 附源码

/**
 * Excel标记类
 * @author JueYue
 *  2015年10月31日 下午9:31:47
 */
public interface IExcelModel {

    /**
     * 获取错误数据
     * @return
     */
    public String getErrorMsg();

    /**
     *  设置错误信息
     * @param errorMsg
     */
    public void setErrorMsg(String errorMsg);

}

我这里只实现了 IExcelDataModel 接口

3.编写导入工具类 网上有很多
@Slf4j
public class ExcelUtils {
    public static <T> List<T> importExcel(MultipartFile file, Integer titleRows, Integer headerRows,
                                          Class<T> pojoClass, HttpServletResponse response) {
        if (file == null) {
            return null;
        }
        ImportParams params = new ImportParams();
        params.setTitleRows(titleRows);
        params.setHeadRows(headerRows);
        params.setNeedVerify(true);
        ExcelImportResult<T> result = null;
        try {
            result = ExcelImportUtil.importExcelMore(file.getInputStream(), pojoClass, params);
        } catch (NoSuchElementException e) {
        	// 日志记录错误
            log.error("excel cannot empty:", e.getMessage());
            BusinessException.throwBusinessException(MsgEnum.EXCEL_CANNOT_EMPIT);
        } catch (Exception e) {
            // 日志记录错误
            log.error("excel import failed:", e.getMessage());
            BusinessException.throwBusinessException(MsgEnum.EXCEL_IMPORT_FAILED);
        }
        if (JudgeUtils.isNull(result)) {
            return null;
        }
        if (result.isVerifyFail()) {
            // 如果有错误,把错误数据返回到前端(让前端下载一个错误的excel)
            Workbook failWorkbook = result.getFailWorkbook();
            downLoadExcel("error_record.xls", response, failWorkbook);
            // 一个自定义枚举 错误信息的
            BusinessException.throwBusinessException(MsgEnum.EXCEL_RECORD_ILLEGAL);
        }
        return result.getList();
    }

    public static void downLoadExcel(String fileName, HttpServletResponse response, Workbook workbook) {
        try {
            fileName = new String(fileName.getBytes(StandardCharsets.UTF_8), StandardCharsets.ISO_8859_1);
            response.setCharacterEncoding("utf-8");
            response.setHeader("content-Type", "application/octet-stream");
            response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
        	// 一个自定义枚举 错误信息的
            BusinessException.throwBusinessException(MsgEnum.IO_EXCEPTION);
        }
    }
}
4.编写Controller
 @PostMapping("/batch/import")
 public void batchImport(@RequestParam("file") MultipartFile file){
     List<CertificateConfigCluster> certificateConfigClustersList = ExcelUtils.importExcel(file, 0, 2, CertificateConfigCluster.class, response);
 }
至此便可以成功获取到xls表格中的数据 至于各位要怎么处理该数据 做什么业务 service 就随自己发挥了 无非就是一个增加操作 这边在附上一张idea 集合数据图

如下
在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值