easypoi Excel文件导入

1、MAVEN依赖

 <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.3</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.3</version>
        </dependency>

2、class

@Data
public class ProjectSrcDetailExcelDTO{


    /**明细编号*/
    @Excel(name = "编号")
    private String detailNo;
     /**明细名称*/
     @Excel(name = "名称")
    private String detailName;
    /**明细规格、技术参数及要求*/
    @Excel(name = "规格、技术参数及要求")
    private String detailRequire;
    /**明细单位*/
    @Excel(name = "单位")
    private String detailUnit;
    /**明细数量*/
    @Excel(name = "数量")
    private String detailNumber;
    /**明细建议品牌*/
    @Excel(name = "建议品牌")
    private String detailBrand;
    /**明细建议型号*/
    @Excel(name = "建议型号")
    private String detailModel;
    /**明细备注*/
    @Excel(name = "备注")
    private String detailDesc;

}

3、Service

public ApiResponse saveAll(MultipartFile file, String proNo, String srcTitle) throws Exception {
        ProjectSrc projectSrc = new ProjectSrc();
        projectSrc.setProNo(proNo);
        projectSrc.setSrcTitle(srcTitle);
        ProjectSrc projectSrcResult = projectSrcRepository.save(projectSrc);
        ImportParams params = new ImportParams();
        params.setHeadRows(1);
        List<ProjectSrcDetailExcelDTO> list = ExcelImportUtil.importExcel(
                file.getInputStream(),ProjectSrcDetailExcelDTO.class, params);
        if (list != null && list.size() > 0) {
            list.forEach(proDetailDto -> {
                ProjectSrcDetail proDetail = modelMapper.map(proDetailDto, ProjectSrcDetail.class);
                proDetail.setSrcId(projectSrcResult.getId());
                proDetail.setDetailState("采购中");
                projectSrcDetailRepository.save(proDetail);
            });
        }
        return ApiResponse.ofMessage(HttpStatus.SC_OK, "添加成功");
    }

4、Excel模板

这里写图片描述

5、其他写法

public void test2() {
        ImportParams params = new ImportParams();
        params.setTitleRows(1);
        params.setHeadRows(1);
        long start = new Date().getTime();
        List<ProjectSrcDetailExcelDTO> list = ExcelImportUtil.importExcel(
                new File(PoiPublicUtil.getWebRootPath("import/ExcelExportMsgClient.xlsx")),
                ProjectSrcDetailExcelDTO.class, params);
        System.out.println(new Date().getTime() - start);
        System.out.println(list.size());
        System.out.println(ReflectionToStringBuilder.toString(list.get(0)));
    }

详情请访问官网http://www.afterturn.cn/

posted @ 2023-07-21 06:53  路暝月  阅读(47)  评论(0编辑  收藏  举报  来源