使用EasyExcel导出Excel

gradle添加依赖

compile 'com.alibaba:easyexcel:2.2.11'

导入Excel样例模板

在这里插入图片描述

代码示例

Excel文件导入对应的模板实体,ExcelProperty内容要与导入的Excel列名保持一致,不一致匹配不上

package com.yshenghuo.entity.wrapper;

import com.alibaba.excel.annotation.ExcelProperty;

/**
 * @description: 检测数据导入
 * @author: haoxueyang
 * @create: 2022-01-11 20:39
 */
public class DetectDataExcel {

    /**
     * 模块商代码
     */
    @ExcelProperty("模块商代码")
    private String modelNo;
    /**
     * 模块商名称
     */
    @ExcelProperty("模块商名称")
    private String modelName;
    /**
     * 生产地址
     */
    @ExcelProperty("模块商地址")
    private String address;
    /**
     * 生产批次
     */
    @ExcelProperty("批次号")
    private String prodBatch;
    /**
     * 海尔专用号(客户编号)
     */
    @ExcelProperty("客户编号")
    private String prodNo;
    /**
     * 物料名称
     */
    @ExcelProperty("产品名称")
    private String prodName;
    /**
     * 生产线体
     */
    @ExcelProperty("生产线体")
    private String lineCode;
    /**
     * 二维码 oid编码
     */
    @ExcelProperty("OID编码")
    private String qrCode;
    /**
     * 检测时间
     */
    @ExcelProperty("测试时间")
    private String testTime;
    /**
     * 检测结果
     */
    @ExcelProperty("检测结果")
    private String testResult;
    /**
     * 产品类别
     */
    @ExcelProperty("产品类别")
    private String productionType;
    /**
     * 电压
     */
    @ExcelProperty("电压(V)")
    private Double voltage;
    /**
     * 电流
     */
    @ExcelProperty("电流(A)")
    private Double electricity;
    /**
     * 备注
     */
    @ExcelProperty("备注")
    private String remark;

    @ExcelProperty("功率(W)")
    private String testData;

    @ExcelProperty("序号")
    private Integer importRank;

    @ExcelProperty("产品编码")
    private String uniqueCode;

    @ExcelProperty("标准要求")
    private String standard;

    @ExcelProperty("环境温度")
    private String environmentTemperature;

    @ExcelProperty("在线检测项目编码")
    private String onlineTestProjectCode;

    public String getStandard() {
        return standard;
    }

    public void setStandard(String standard) {
        this.standard = standard;
    }

    public String getEnvironmentTemperature() {
        return environmentTemperature;
    }

    public void setEnvironmentTemperature(String environmentTemperature) {
        this.environmentTemperature = environmentTemperature;
    }

    public String getOnlineTestProjectCode() {
        return onlineTestProjectCode;
    }

    public void setOnlineTestProjectCode(String onlineTestProjectCode) {
        this.onlineTestProjectCode = onlineTestProjectCode;
    }

    public String getUniqueCode() {
        return uniqueCode;
    }

    public void setUniqueCode(String uniqueCode) {
        this.uniqueCode = uniqueCode;
    }

    public Integer getImportRank() {
        return importRank;
    }

    public void setImportRank(Integer importRank) {
        this.importRank = importRank;
    }

    public String getModelNo() {
        return modelNo;
    }

    public void setModelNo(String modelNo) {
        this.modelNo = modelNo;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getProdBatch() {
        return prodBatch;
    }

    public void setProdBatch(String prodBatch) {
        this.prodBatch = prodBatch;
    }

    public String getProdNo() {
        return prodNo;
    }

    public void setProdNo(String prodNo) {
        this.prodNo = prodNo;
    }

    public String getProdName() {
        return prodName;
    }

    public void setProdName(String prodName) {
        this.prodName = prodName;
    }

    public String getLineCode() {
        return lineCode;
    }

    public void setLineCode(String lineCode) {
        this.lineCode = lineCode;
    }

    public String getQrCode() {
        return qrCode;
    }

    public void setQrCode(String qrCode) {
        this.qrCode = qrCode;
    }

    public String getTestTime() {
        return testTime;
    }

    public void setTestTime(String testTime) {
        this.testTime = testTime;
    }

    public String getTestResult() {
        return testResult;
    }

    public void setTestResult(String testResult) {
        this.testResult = testResult;
    }

    public String getProductionType() {
        return productionType;
    }

    public void setProductionType(String productionType) {
        this.productionType = productionType;
    }

    public Double getVoltage() {
        return voltage;
    }

    public void setVoltage(Double voltage) {
        this.voltage = voltage;
    }

    public Double getElectricity() {
        return electricity;
    }

    public void setElectricity(Double electricity) {
        this.electricity = electricity;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public String getTestData() {
        return testData;
    }

    public void setTestData(String testData) {
        this.testData = testData;
    }

    @Override
    public String toString() {
        return "DetectDataExcel{" +
                "modelNo='" + modelNo + '\'' +
                ", modelName='" + modelName + '\'' +
                ", address='" + address + '\'' +
                ", prodBatch='" + prodBatch + '\'' +
                ", prodNo='" + prodNo + '\'' +
                ", prodName='" + prodName + '\'' +
                ", lineCode='" + lineCode + '\'' +
                ", qrCode='" + qrCode + '\'' +
                ", testTime='" + testTime + '\'' +
                ", testResult='" + testResult + '\'' +
                ", productionType='" + productionType + '\'' +
                ", voltage=" + voltage +
                ", electricity=" + electricity +
                ", remark='" + remark + '\'' +
                ", testData='" + testData + '\'' +
                '}';
    }
}

字段实体,与数据库字段一一对应

package com.yshenghuo.entity;

import com.baomidou.mybatisplus.annotation.FieldStrategy;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.activerecord.Model;

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

/**
 * <p>
 * 检测数据表
 * </p>
 *
 * @author yshenghuo
 * @since 2021-11-25
 */
@TableName("hq_detect_data")
public class DetectData extends Model<DetectData> {

    private static final long serialVersionUID = 1L;

    @TableId
    private Long id;
    /**
     * 模块商代码
     */
    @TableField("model_no")
    private String modelNo;
    /**
     * 模块商名称
     */
    @TableField("model_name")
    private String modelName;
    /**
     * 生产工厂全称
     */
    @TableField("produce_fac_full_name")
    private String produceFacFullName;
    /**
     * 生产地址
     */
    @TableField("address")
    private String address;
    /**
     * 生产批次
     */
    @TableField("prod_batch")
    private String prodBatch;
    /**
     * 海尔专用号(客户编号)
     */
    @TableField("prod_no")
    private String prodNo;
    /**
     * 物料名称
     */
    @TableField("prod_name")
    private String prodName;
    /**
     * 生产线体
     */
    @TableField("line_code")
    private String lineCode;
    /**
     * 产品唯一码/产品编码
     */
    @TableField("unique_code")
    private String uniqueCode;
    /**
     * 二维码 oid编码
     */
    @TableField("qr_code")
    private String qrCode;
    /**
     * 检测时间
     */
    @TableField("test_time")
    private Date testTime;
    /**
     * 在线检测项目编码
     */
    @TableField("online_test_project_code")
    private String onlineTestProjectCode;
    /**
     * 在线检测项目
     */
    @TableField("online_test_project")
    private String onlineTestProject;
    /**
     * 标准要求
     */
    @TableField("standard")
    private String standard;
    /**
     * 检测数据
     */
    @TableField("test_data")
    private Double testData;
    /**
     * 检测结果:1合格 2不合格
     */
    @TableField("test_result")
    private Integer testResult;
    /**
     * 同步状态:10未审核 20已审核 30同步成功 40同步失败
     */
    @TableField("sync_status")
    private Integer syncStatus;
    /**
     * 同步时间
     */
    @TableField("sync_time")
    private Date syncTime;
    /**
     * 同步原因(失败时回填)
     */
    @TableField("sync_msg")
    private String syncMsg;
    /**
     * 产品类别:
     */
    @TableField("production_type")
    private String productionType;
    /**
     * 环境温度
     */
    @TableField(value = "environment_temperature", strategy = FieldStrategy.IGNORED, el = "environmentTemperature,jdbcType=VARCHAR")
    private Float environmentTemperature;
    /**
     * 电压
     */
    @TableField(value = "voltage", strategy = FieldStrategy.IGNORED, el = "voltage,jdbcType=VARCHAR")
    private Double voltage;
    /**
     * 电流
     */
    @TableField(value = "electricity", strategy = FieldStrategy.IGNORED, el = "electricity,jdbcType=VARCHAR")
    private Double electricity;
    /**
     * 备注
     */
    @TableField("remark")
    private String remark;

    @TableField("create_time")
    private Date createTime;

    @TableField("update_time")
    private Date updateTime;

    @TableField("del_flag")
    private Integer delFlag;
    /**
     * 导入序号
     */
    @TableField(value = "import_rank", strategy = FieldStrategy.IGNORED, el = "importRank,jdbcType=VARCHAR")
    private Integer importRank;

    public Integer getImportRank() {
        return importRank;
    }

    public void setImportRank(Integer importRank) {
        this.importRank = importRank;
    }

    public Integer getDelFlag() {
        return delFlag;
    }

    public void setDelFlag(Integer delFlag) {
        this.delFlag = delFlag;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public String getProductionType() {
        return productionType;
    }

    public void setProductionType(String productionType) {
        this.productionType = productionType;
    }

    public Float getEnvironmentTemperature() {
        return environmentTemperature;
    }

    public void setEnvironmentTemperature(Float environmentTemperature) {
        this.environmentTemperature = environmentTemperature;
    }

    public Double getVoltage() {
        return voltage;
    }

    public void setVoltage(Double voltage) {
        this.voltage = voltage;
    }

    public Double getElectricity() {
        return electricity;
    }

    public void setElectricity(Double electricity) {
        this.electricity = electricity;
    }

    public String getRemark() {
        return remark;
    }

    public void setRemark(String remark) {
        this.remark = remark;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getModelNo() {
        return modelNo;
    }

    public void setModelNo(String modelNo) {
        this.modelNo = modelNo;
    }

    public String getModelName() {
        return modelName;
    }

    public void setModelName(String modelName) {
        this.modelName = modelName;
    }

    public String getProduceFacFullName() {
        return produceFacFullName;
    }

    public void setProduceFacFullName(String produceFacFullName) {
        this.produceFacFullName = produceFacFullName;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getProdBatch() {
        return prodBatch;
    }

    public void setProdBatch(String prodBatch) {
        this.prodBatch = prodBatch;
    }

    public String getProdNo() {
        return prodNo;
    }

    public void setProdNo(String prodNo) {
        this.prodNo = prodNo;
    }

    public String getProdName() {
        return prodName;
    }

    public void setProdName(String prodName) {
        this.prodName = prodName;
    }

    public String getLineCode() {
        return lineCode;
    }

    public void setLineCode(String lineCode) {
        this.lineCode = lineCode;
    }

    public String getUniqueCode() {
        return uniqueCode;
    }

    public void setUniqueCode(String uniqueCode) {
        this.uniqueCode = uniqueCode;
    }

    public String getQrCode() {
        return qrCode;
    }

    public void setQrCode(String qrCode) {
        this.qrCode = qrCode;
    }


    public String getOnlineTestProjectCode() {
        return onlineTestProjectCode;
    }

    public void setOnlineTestProjectCode(String onlineTestProjectCode) {
        this.onlineTestProjectCode = onlineTestProjectCode;
    }

    public String getOnlineTestProject() {
        return onlineTestProject;
    }

    public void setOnlineTestProject(String onlineTestProject) {
        this.onlineTestProject = onlineTestProject;
    }

    public String getStandard() {
        return standard;
    }

    public void setStandard(String standard) {
        this.standard = standard;
    }

    public Double getTestData() {
        return testData;
    }

    public void setTestData(Double testData) {
        this.testData = testData;
    }

    public Integer getTestResult() {
        return testResult;
    }

    public void setTestResult(Integer testResult) {
        this.testResult = testResult;
    }

    public Integer getSyncStatus() {
        return syncStatus;
    }

    public void setSyncStatus(Integer syncStatus) {
        this.syncStatus = syncStatus;
    }

    public Date getTestTime() {
        return testTime;
    }

    public void setTestTime(Date testTime) {
        this.testTime = testTime;
    }

    public Date getSyncTime() {
        return syncTime;
    }

    public void setSyncTime(Date syncTime) {
        this.syncTime = syncTime;
    }

    public String getSyncMsg() {
        return syncMsg;
    }

    public void setSyncMsg(String syncMsg) {
        this.syncMsg = syncMsg;
    }

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    @Override
    public String toString() {
        return "DetectData{" +
                "id=" + id +
                ", modelNo=" + modelNo +
                ", modelName=" + modelName +
                ", produceFacFullName=" + produceFacFullName +
                ", address=" + address +
                ", prodBatch=" + prodBatch +
                ", prodNo=" + prodNo +
                ", prodName=" + prodName +
                ", lineCode=" + lineCode +
                ", uniqueCode=" + uniqueCode +
                ", qrCode=" + qrCode +
                ", testTime=" + testTime +
                ", onlineTestProjectCode=" + onlineTestProjectCode +
                ", onlineTestProject=" + onlineTestProject +
                ", standard=" + standard +
                ", testData=" + testData +
                ", testResult=" + testResult +
                ", syncStatus=" + syncStatus +
                ", syncTime=" + syncTime +
                ", syncMsg=" + syncMsg +
                "}";
    }
}

实现层导入方法,每读Excel一行数据,就会进入一次invoke方法,读取完之后进入doAfterAllAnalysed

    //导入excel
    @Override
    public ActionResult importData(MultipartFile file) {
        ActionResult actionResult = new ActionResult();
        List<DetectData> saveList = new ArrayList<>();
        try {
            if (file == null) {
                return actionResult.setError("Excel表为空");
            }
            EasyExcel.read(file.getInputStream(), DetectDataExcel.class, new AnalysisEventListener<DetectDataExcel>() {
                int row = 1;

                @Override
                public void invoke(DetectDataExcel data, AnalysisContext context) {
                    DetectData entity = new DetectData();
                    BeanUtils.copyProperties(data, entity);
                    row++;
                    SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                    List<Double> standardList = new ArrayList<>();
                    //序号
                    if (data.getImportRank() == null) {
                        throw new RuntimeException("第" + row + "行,序号为空");
                    }
                    //模块商代码
                    if (StringUtils.isEmpty(data.getModelNo())) {
                        throw new RuntimeException("第" + row + "行,模块商代码为空");
                    }
                    //模块商名称
                    if (StringUtils.isEmpty(data.getModelName())) {
                        throw new RuntimeException("第" + row + "行,模块商名称为空");
                    }
                    //模块商地址
                    if (StringUtils.isEmpty(data.getAddress())) {
                        throw new RuntimeException("第" + row + "行,模块商地址为空");
                    }
                    //产品类别
                    //批次号
                    if (StringUtils.isEmpty(data.getProdBatch())) {
                        throw new RuntimeException("第" + row + "行,批次号为空");
                    }
                    //客户编号
                    if (StringUtils.isEmpty(data.getProdNo())) {
                        throw new RuntimeException("第" + row + "行,客户编号为空");
                    }
                    //产品编码
                    if (StringUtils.isEmpty(data.getUniqueCode())) {
                        throw new RuntimeException("第" + row + "行,产品编码为空");
                    }
                    //产品名称
                    if (StringUtils.isEmpty(data.getProdName())) {
                        throw new RuntimeException("第" + row + "行,产品名称为空");
                    }
                    //生产线体
                    if (StringUtils.isEmpty(data.getLineCode())) {
                        throw new RuntimeException("第" + row + "行,生产线体为空");
                    }
                    //OID编码
                    if (StringUtils.isEmpty(data.getQrCode())) {
                        throw new RuntimeException("第" + row + "行,OID编码为空");
                    }
                    //标准要求
                    if (StringUtils.isEmpty(data.getStandard())) {
                        throw new RuntimeException("第" + row + "行,标准要求为空");
                    }
                    try {
                        String[] arr = data.getStandard().split("-");
                        if (arr.length != 2) {
                            throw new RuntimeException("第" + row + "行,标准要求格式不正确,请按照XXX-XXX格式填写");
                        }
                        standardList.add(Double.parseDouble(arr[0]));
                        standardList.add(Double.parseDouble(arr[1]));
                    } catch (Exception e) {
                        log.error("第" + row + "行,标准要求:" + data.getStandard());
                        throw new RuntimeException("第" + row + "行,标准要求格式不正确,请按照XXX-XXX格式填写,XXX-为数值型,例如7.03-8.23");
                    }
                    //环境温度
                    if (StringUtils.isEmpty(data.getEnvironmentTemperature())) {
                        throw new RuntimeException("第" + row + "行,环境温度为空");
                    }
                    try {
                        entity.setEnvironmentTemperature(Float.valueOf(data.getEnvironmentTemperature()));
                    } catch (Exception e) {
                        throw new RuntimeException("第" + row + "行,环境温度请填写数值型");
                    }
                    //测试时间
                    if (StringUtils.isEmpty(data.getTestTime())) {
                        throw new RuntimeException("第" + row + "行,测试时间为空");
                    }
                    log.error("testTime:" + data.getTestTime());
                    try {
                        if (data.getTestTime().length() <= 16) {//2021/12/14 13:51
                            entity.setTestTime(simpleDateFormat.parse(data.getTestTime() + ":00"));
                        } else {
                            entity.setTestTime(simpleDateFormat.parse(data.getTestTime()));
                        }
                    } catch (ParseException e) {
                        e.printStackTrace();
                        throw new RuntimeException("第" + row + "行,测试时间格式不正确,请填写日期格式,例如2021/12/14 13:51:07");
                    }
                    //电压(V)
                    //电流(A)
                    //功率(W) 检测数据
                    if (StringUtils.isEmpty(data.getTestData())) {
                        throw new RuntimeException("第" + row + "行,功率(W)为空");
                    }
                    try {
                        entity.setTestData(Double.valueOf(data.getTestData()));
                    } catch (Exception e) {
                        log.error("第" + row + "行,功率(W)格式不正确,请填写数值型");
                        throw new RuntimeException("第" + row + "行,功率(W)格式不正确,请填写数值型");
                    }
                    if (entity.getTestData().compareTo(standardList.get(0)) >= 0 && standardList.get(1).compareTo(entity.getTestData()) >= 0) {
                        entity.setTestResult(Constant.DetectDataTestData.Qualified);//测试结果
                    } else {
                        entity.setTestResult(Constant.DetectDataTestData.UnQualified);//测试结果
                    }
                    log.error("entity.getTestData() :" + entity.getTestData().toString());
                    log.error("standardList.get(0) :" + standardList.get(0));
                    log.error("standardList.get(1) :" + standardList.get(1));
                    log.error("entity.getTestData().compareTo(standardList.get(0)) :" + entity.getTestData().compareTo(standardList.get(0)));
                    log.error("standardList.get(1).compareTo(entity.getTestData()) :" + standardList.get(1).compareTo(entity.getTestData()));
                    //在线检测项目编码
                    if (StringUtils.isEmpty(data.getOnlineTestProjectCode())) {
                        throw new RuntimeException("第" + row + "行,在线检测项目编码为空");
                    }
                    //检测结果
                    //备注
                    entity.setDelFlag(0);
                    entity.setCreateTime(new Date());//创建时间
                    entity.setSyncStatus(Constant.DetectDataSyncStatus.UnAudit);//未审核
                    saveList.add(entity);
                }

                @Override
                public void doAfterAllAnalysed(AnalysisContext context) {
                    System.err.println("结束导入");

                }
            }).sheet().doRead();
            if (saveList != null && saveList.size() > 0) {
                this.saveOrUpdateBatch(saveList);
            }
            actionResult.setSuccess(null, "导入成功");
        } catch (Exception e) {
            e.printStackTrace();
            actionResult.setError(e.getMessage());
        }
        return actionResult;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值