一,EasyExcel使用
1.1 添加EasyExcel依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>easyexcel</artifactId>
<version>2.2.7</version>
</dependency>
1.2 导入实体
/**
* 导入模板
*/
@Data
public class FrozenBuckleImportDTO implements Serializable {
@ExcelProperty(value = "客户号*",index = 0)
@ExcelDataValid(message = "客户号不能为空,请填写后重新导入!")
@Size(max = 64, message = "客户号 不能超过64")
private String custNo;
@ExcelProperty(value = "客户名称*",index = 1)
@ExcelDataValid(message = "客户名称不能为空,请填写后重新导入!")
@Size(max = 128, message = "客户名称 不能超过128")
private String custCnName;
@ExcelProperty(value = "客户类型",index = 2,converter = WhetherConverter.class)
@ExcelDataValid(message = "客户类型不能为空,请填写后重新导入!")
@Size(max = 12, message = "客户类型 不能超过12")
@DictTypeProperty("CustRisk_CustClass")
private String custClass;
}
1.3 导入实现
a. Controller
@ApiOperation(value = "导入", notes = "导入")
@PostMapping(value = "/importExcel", headers = {
"content-type=multipart/form-data"})
public ObjectRestResponse importExcel(@RequestParam("file") MultipartFile file) {
if (file.isEmpty()) {
return new ObjectRestResponse(HttpStatus.HTTP_INTERNAL_ERROR, "文件不能为空", null );
}
try {
return frozenBuckleService.importExcelInfo(file);
} catch (Exception e) {
log.error("导入异常:", e);
String substring = e.getMessage().substring(66);
String s = substring.replaceAll("[a-zA-Z={}]", "");
return new ObjectRestResponse(HttpStatus.HTTP_INTERNAL_ERROR, "导入异常:"+s, null );
}
}
b. 具体实现方法
@Override
public ObjectRestResponse importExcelInfo(