excel导入基于Easypoi一对多导入(实现合并单元格)

2022-07-27,,,,

EasyPoi一对多导入

EasyPoi官方文档:(http://easypoi.mydoc.io/)

前言

利用EasyPoi实现一对多的表格导入,主要注解@ExcelCollection

一、导入表格示例

二、使用步骤

1.pom依赖

springboot整合EasyPoi

 <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-spring-boot-starter</artifactId>
            <version>4.1.3</version>
        </dependency>

2.导入对应的实体

@Excel:作用在字段上面,对应Excel的某一列的描述。

@ExcelCollection:集合,主要针对一对多的导出。

import cn.afterturn.easypoi.excel.annotation.Excel;
import cn.afterturn.easypoi.excel.annotation.ExcelCollection;
import lombok.Data;

import java.util.List;
@Data
public class NewFunctionExcelVo {
    /**
     * 功能编号
     */
    @Excel(name = "一级功能编号", needMerge = true, width = 30)
    private String oneLevelNo;
    /**
     * 功能名称
     */
    @Excel(name = "一级功能", needMerge = true, width = 35)
    private String oneLevelName;

    @ExcelCollection(name = "子功能及描述")
    private List<ChildrenExcelVo> datChildrenFunction;
}
import cn.afterturn.easypoi.excel.annotation.Excel;
import lombok.Data;

@Data
public class ChildrenExcelVo {
    @Excel(name = "二级功能编号", width = 35)
    private String twoLevelNo;

    @Excel(name = "二级功能", width = 35)
    private String twoLevelName;

    @Excel(name = "三级功能编号", width = 35)
    private String threeLevelNo;

    @Excel(name = "三级功能", width = 35)
    private String threeLevelName;

    @Excel(name = "四级功能编号", width = 35)
    private String fourLevelNo;

    @Excel(name = "四级功能", width = 35)
    private String fourLevelName;

    /**
     * 功能属性,关联数据字典“功能属性“
     */
    @Excel(name = "属性", width = 30)
    private String propertyName;

    /**
     * 说明
     */
    @Excel(name = "说明")
    private String support;

    /**
     * 描述
     */
    @Excel(name = "描述", width = 40)
    private String description;


    /**
     * 特性1
     */
    @Excel(name = "特性1", width = 40)
    private String exampleName;

    /**
     * 特性2
     */
    @Excel(name = "特性2", width = 40)
    private String service;
}

Test

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import org.junit.Test;

import java.io.FileInputStream;
import java.util.List;

public class test {
    @Test
    public void importQuestion() throws Exception {
        FileInputStream fis = new FileInputStream("D:\\测试文件.xlsx");
        ImportParams params = new ImportParams();
        params.setHeadRows(2);
        params.setTitleRows(0);
        List<DatNewFunctionExcelVo> functionList = ExcelImportUtil.importExcel(fis,
                NewFunctionExcelVo.class, params);
    }
}

导入最终数据结构

需要注意的是,如果当前单元格处于一对多状态,那么只有第一条是有数据,其余都为null。
举例:此处直接贴图直抒胸臆


总结

此前在网上也搜索了很多一对多的示例,导出的文章较多,并且不太符合本项目的需求,为此写了这篇又臭又长的博客供大家参考,导入导出最重要的就是实体类结合表格进行清楚明了的对应,其次结合EasyPoi注解,希望对大家有所帮助。

本文地址:https://blog.csdn.net/qq_21569575/article/details/110195105

《excel导入基于Easypoi一对多导入(实现合并单元格).doc》

下载本文的Word格式文档,以方便收藏与打印。