新增excel导入结果对象和导入错误处理类

This commit is contained in:
wenc000 2020-04-23 22:30:11 +08:00
parent 9e37db05ca
commit f52e56df56
2 changed files with 126 additions and 0 deletions

View File

@ -0,0 +1,58 @@
package com.cm.common.plugin.excel.error;
import com.alibaba.excel.EasyExcel;
import com.cm.common.plugin.service.file.IFileService;
import com.cm.common.utils.UUIDUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: AbstractErrorExcelHandler
* @Description: Excel错误处理器
* @Author: WangGeng
* @Date: 2020/4/23 9:56 下午
* @Version: 1.0
**/
public abstract class AbstractErrorExcelHandler<T> {
private static final Logger LOG = LoggerFactory.getLogger(AbstractErrorExcelHandler.class);
private IFileService fileService;
public AbstractErrorExcelHandler(IFileService fileService) {
this.fileService = fileService;
}
public String saveErrorExcel(List<T> excelErrors) {
LOG.debug("生成错误信息Excel");
// 文件路径
String errorExcelPath = fileService.getUploadExcelErrorPath();
File errorExcelPathFileFolder = new File(errorExcelPath);
if (!errorExcelPathFileFolder.exists()) {
errorExcelPathFileFolder.mkdirs();
}
// 文件名
String fileName = String.format("%s.xls", UUIDUtil.get32UUID());
// 表格头
File file = new File(String.format("%s/%s", errorExcelPath, fileName));
EasyExcel.write(file).sheet("错误清单").head(excelHeaderNames()).doWrite(excelErrors);
Map<String, Object> params = new HashMap<>(8);
fileService.saveUploadErrorExcelFileInfo(fileName, errorExcelPath, file.length(), params);
return params.get("fileId").toString();
}
/**
* 错误表头
*
* @return
*/
public abstract List<List<String>> excelHeaderNames();
}

View File

@ -0,0 +1,68 @@
package com.cm.common.result;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
/**
* When you feel like quitting. Think about why you started
* 当你想要放弃的时候想想当初你为何开始
*
* @ClassName: UploadExcelResultDTO
* @Description: 上传Excel结果
* @Author: WangGeng
* @Date: 2020/1/6 10:41 下午
* @Version: 1.0
**/
@ApiModel
public class UploadExcelResultDTO {
@ApiModelProperty(name = "failedCount", value = "失败数量")
private Integer failedCount;
@ApiModelProperty(name = "usedTime", value = "耗时")
private Long usedTime;
@ApiModelProperty(name = "errorExcel", value = "错误提示Excel路径")
private String errorExcel;
public UploadExcelResultDTO(Integer failedCount, Long usedTime, String errorExcel) {
this.failedCount = failedCount;
this.usedTime = usedTime;
this.errorExcel = errorExcel;
}
public Integer getFailedCount() {
return failedCount;
}
public void setFailedCount(Integer failedCount) {
this.failedCount = failedCount;
}
public Long getUsedTime() {
return usedTime;
}
public void setUsedTime(Long usedTime) {
this.usedTime = usedTime;
}
public String getErrorExcel() {
return errorExcel == null ? "" : errorExcel.trim();
}
public void setErrorExcel(String errorExcel) {
this.errorExcel = errorExcel;
}
@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"failedCount\":")
.append(failedCount);
sb.append(",\"usedTime\":")
.append(usedTime);
sb.append(",\"errorExcel\":")
.append("\"").append(errorExcel).append("\"");
sb.append('}');
return sb.toString();
}
}