对于第一次接触到这类需求来说,还是比较懵逼的,因为码龄也不是很长,但是幸不辱命,经过一系列的努力,还是成功写出,为了下次使用方便,直接搬代码;
首先如果要从外部文件中获取数据,就涉及到属性字段的对应关系,请看下图,每个属性都需要加上 @ExcelProperty()注解,目的是为了和文件的字段进行对应,
@Data
public class OrderExcelImportDTO extends BaseImport {
@ExcelProperty("id")
private Long refundId;
@ExcelProperty("单号")
@NotEmpty(message = "单号不能为空")
@Size(max = 32, message = "单号不能超过32位")
@Unique
private String refundNo;
@ExcelProperty("售后单号")
@NotEmpty(message = "**单号不能为空")
@Size(max = 32, message = "**单号不能超过32位")
@Unique
private String sourceAfterSaleOrderNo;
}
该类需继承BaseImport,BaseImport是自己写的类,代码如下:
import java.io.Serializable;
public class BaseImport implements Serializable {
private static final long serialVersionUID = 1L;
private String errorMsg;
private int rowIndex;
public BaseImport() {
}
public String getErrorMsg() {
return this.errorMsg;
}
public int getRowIndex() {
return this.rowIndex;
}
public void setErrorMsg(String errorMsg) {
this.errorMsg = errorMsg;
}
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
public boolean equals(Object o) {
if (o == this) {
return true;
} else if (!(o instanceof BaseImport)) {
return false;
} else {
BaseImport other = (BaseImport)o;
if (!other.canEqual(this)) {
return false;
} else if (this.getRowIndex() != other.getRowIndex()) {
return false;
} else {