springboot, easypoi导入导出示例及配置

配置导入easypoi 包

 <!-- easypoi -->
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-base</artifactId>
            <version>3.0.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-web</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>cn.afterturn</groupId>
            <artifactId>easypoi-annotation</artifactId>
            <version>3.0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-scratchpad</artifactId>
            <version>3.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>3.12</version>
        </dependency>
        <dependency>
            <groupId>org.apache.ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.8.2</version>
        </dependency>
        <!-- easypoi -->

实体类文件配置

@Excel(name = "产品名称",orderNum = "1")
package com.taixin.rail.vo;

import cn.afterturn.easypoi.excel.annotation.Excel;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;
import java.util.Date;


@Data
public class HarmlessVo implements Serializable {
    private static final long serialVersionUID = 1L;

    @ApiModelProperty(value = "id")
    private Integer id;

    @ApiModelProperty(value = "产品名称")
    @Excel(name = "产品名称",orderNum = "1")
    private String product;

    @ApiModelProperty(value = "屠宰场名称")
    @Excel(name = "屠宰场名称",orderNum = "2")
    private String slaughterhouse;

    @ApiModelProperty(value = "耳标")
    @Excel(name = "耳标",orderNum = "3")
    private String earTag;

    @ApiModelProperty(value = "数量")
    @Excel(name = "数量",orderNum = "4")
    private Integer harmlessNo;

    @ApiModelProperty(value = "操作时间")
    @Excel(name = "操作时间",orderNum = "5")
    private String createTime;



}

导出使用

 @PostMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response, @RequestBody HarmlessQo qo) {
         //获取要导出的数据
        List<HarmlessVo> list = harmlessService.exportExcel(qo);
        ExcelUtil.exportExcel(list, qo.getXlsTitle(), qo.getXlsTitle(), HarmlessVo.class, qo.getXlsTitle() + ".xls", response);
    }

导入使用

/**
     * 导入
     *
     * @param createBy
     * @param file
     * @return
     */
    @PostMapping(value = "/importExcel")
    @ResponseBody
    public Result importExcel(@RequestParam("file") MultipartFile file, @RequestParam("createBy") String createBy) {
        if (file.isEmpty()) {
            return Result.error(404, "文件不能为空");
        }
        return Result.ok(harmlessService.importExcel(file, createBy));
    }

 @Override
    public Integer importExcel(MultipartFile file, String createBy) {
        List<HarmlessVo> list = ExcelUtil.importExcel(file, 0, 1, HarmlessVo.class);
      //添加数据
        Integer num = harmlessMapper.importExcel(list);
        return num;
    }

工具类下载  ExcelUtil.java 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值