2021-02-09-SpringBoot integrates autopoi to realize the import and export function of excel table

SpringBoot integrates autopoi

pom.xml

  • Note that the private server is placed outside
   <!--私服-->
    <distributionManagement>
        <repository>
            <id>jeecg</id>
            <name>jeecg Repository</name>
            <url>http://maven.jeecg.com:8090/nexus/content/repositories/jeecg</url>
        </repository>
        <snapshotRepository>
            <id>jeecg-snapshots</id>
            <name>jeecg Snapshot Repository</name>
            <url>http://maven.jeecg.com:8090/nexus/content/repositories/snapshots/</url>
        </snapshotRepository>
    </distributionManagement>
    <dependencies>
       <dependency>
            <groupId>org.jeecgframework</groupId>
            <artifactId>autopoi-web</artifactId>
            <version>1.0.3</version>
            <exclusions>
                <exclusion>
                    <groupId>commons-codec</groupId>
                    <artifactId>commons-codec</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
      </dependencies>

Entity class

  • Just add the two annotations @ExcelTarget and @Excel
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("T_TZSB_FACTORY_OUT_PERSON")
@ExcelTarget(value = "FactoryOutPersonEntity")
public class FactoryOutPersonEntity {
    //id
    @TableId(type = IdType.AUTO)
    private Integer id;
    // 证件编号
    @Excel(name = "证件编号",width = 15)
    private String certificateNum;
    //姓名
    @Excel(name = "姓名",width = 15)
    private String personName;
    //性别
    @Excel(name = "性别",width = 15)
    private String sex;

}

Import function

  • The front end only needs to pass a MultipartFile object, which is the same as normal file upload
    @RequestMapping(value = "/importExcel")
    @ResponseBody
    public ResultEntity<FactoryOutPersonEntity> importExcel(@RequestParam("file") MultipartFile file) {
        log.info("导入厂外人员信息");
        List<FactoryOutPersonEntity> list = null;
        try {
            ImportParams params = new ImportParams();
            //表格标题所在行,计数从0开始
            params.setTitleRows(0);
            //head头部所在行,计数从0开始
            params.setHeadRows(0);
            //表格sheet数量
            //params.setSheetNum(9);
            //最好不要设置为true,否则导入一次excel服务器会重启一次,原因不明
//            params.setNeedSave(false);
            InputStream inputStream = file.getInputStream();
            list = ExcelImportUtil.importExcel(inputStream, FactoryOutPersonEntity.class, params);
            //批量插入,出错回滚
            int insert = factoryOutPersonService.addBatch(list);

        } catch (Exception e) {
            e.printStackTrace();
            return ResultEntity.FAILMsg("失败");
        }
        return ResultEntity.SUCCESSMsg("成功", list);
    }

Export function

  • The export function is also simple. You only need to check the database to get the data collection you want to export, and then pass the object type corresponding to the collection. The front end only needs to access /exportXls to get an excel file.
 @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(FactoryOutPersonEntity fope) {
        log.info("导出厂外人员信息");
        //组建查询条件
        QueryWrapper<FactoryOutPersonEntity> wrapper = new QueryWrapper<>();
        if (!StringUtil.isEmpty(fope.getCertificateNum())) {
            wrapper.eq("CERTIFICATE_NUM", fope.getCertificateNum());
        }
        if (!StringUtil.isEmpty(fope.getPersonName())) {
            wrapper.eq("PERSON_NAME", fope.getPersonName());
        }
        if (!StringUtil.isEmpty(fope.getWorkProject())) {
            wrapper.eq("WORK_PROJECT", fope.getWorkProject());
        }
        List<FactoryOutPersonEntity> factoryOutPersonEntities =   factoryOutPersonService.getFactoryOutPersonEntities(wrapper);

        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        //导出文件名称
        mv.addObject(NormalExcelConstants.FILE_NAME, "厂外人员信息表");
        //注解对象Class
        mv.addObject(NormalExcelConstants.CLASS, FactoryOutPersonEntity.class);
        //自定义导出字段
        mv.addObject(NormalExcelConstants.PARAMS, new ExportParams());
        //导出数据列表
        mv.addObject(NormalExcelConstants.DATA_LIST, factoryOutPersonEntities);
        return mv;
    }

Guess you like

Origin blog.csdn.net/qq_41270550/article/details/113755338