实体类
@Data
@TableName("")
public class EquipmentExcel implements Serializable {
@ExcelProperty(value = {"所属部门"})
@TableField("dept_guid")
private String deptGuid;
}
控制层
@ControllerEndpoint(operation = "上传设备数据", exceptionMessage = "导入Excel失败")
@PostMapping(value = "/uploadExcel", consumes = "multipart/*", headers = "content-type=multipart/form-data")
@ResponseBody
public List<String> uploadEquipmentInfo(MultipartFile file) throws IOException {
return equipmentService.uploadEquipmentInfo(file,deviceCategory,deviceType,status,user);
}
业务层
try{
EasyExcel.read(file.getInputStream(), EquipmentExcel.class, new ExcelModelListener(baseMapper,deviceCategory)).sheet().doRead();
}catch (Exception e) {
StringTokenizer st = new StringTokenizer(e.getMessage(),"|");
while (st.hasMoreElements()) {
Message.add(st.nextElement().toString());
}
}finally {
return Message;
}
监听器
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.exception.ExcelAnalysisException;
import com.alibaba.excel.exception.ExcelDataConvertException;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import java.text.MessageFormat;
import java.util.*;
public class ExcelModelListener extends AnalysisEventListener<EquipmentExcel> {
private static final int BATCH_COUNT = 300;
private int COUNT = 0;
private int duplicateCOUNT = 0;
private List<EquipmentExcel> list = new ArrayList<>();
private List<String> errorMessage = new ArrayList<>();
private EquipmentMapper baseMapper;
private List<DictValue> deviceCategory = new ArrayList<>();
public ExcelModelListener(EquipmentMapper baseMapper, List<DictValue> deviceCategory, List<DictValue> deviceType, List<DictValue> status, CurrentUser user,List Tdept,List Tguiddata) {
this.baseMapper = baseMapper;
this.deviceCategory = deviceCategory;
}
@Override
public void invoke(EquipmentExcel data, AnalysisContext context) {
this.COUNT = this.COUNT + 1;
checkLengthAndNull(data);
checkDiy(data);
list.add(data);
if (list.size() >= BATCH_COUNT) {
saveData();
list.clear();
}
}
@Override
public void doAfterAllAnalysed(AnalysisContext analysisContext) {
saveData();
StringBuilder sb = new StringBuilder();
String countMessage = MessageFormat.format(
"一共{0}条数据,成功导入{1}条数据,重复{2}条数据,失败{3}条数据:",
this.COUNT, this.COUNT - this.duplicateCOUNT - this.errorMessage.size(),
duplicateCOUNT, this.errorMessage.size());
sb.append(countMessage);
if (this.errorMessage.size() > 0) {
errorMessage.forEach((it) -> {
sb.append("|");
sb.append(it);
});
}
throw new ExcelAnalysisException(sb.toString());
}
@Override
public void invokeHeadMap(Map headMap, AnalysisContext context) {
this.headMap = headMap;
}
@Override
public void onException(Exception exception, AnalysisContext context) {
if (exception instanceof ExcelDataConvertException) {
this.COUNT=this.COUNT+1;
ExcelDataConvertException excelDataConvertException = (ExcelDataConvertException) exception;
String columnName = (String) this.headMap.get(excelDataConvertException.getColumnIndex());
int row = excelDataConvertException.getRowIndex();
String cellData = excelDataConvertException.getCellData().toString();
String mes = MessageFormat.format("错误发生在{0}行,{1}列,数据为:{2}", (row+1), columnName, cellData);
errorMessage.add(mes);
}
else {
errorMessage.add(exception.getMessage());
}
}
private void saveData() {
if (!CollectionUtils.isEmpty(list)) {
int originSize = list.size();
list = duplicateRemovalExcel(list);
duplicateRemoval(list);
int duplicateSize = list.size();
this.duplicateCOUNT = this.duplicateCOUNT + (originSize - duplicateSize);
}
if(!CollectionUtils.isEmpty(list)) {
baseMapper.insertexcel(list);
}
}
private void throwException(String message) {
String mes = MessageFormat.format("错误发生在{0}行:{1}", this.COUNT+1,message);
throw new ExcelAnalysisException(mes);
}
private void checkLengthAndNull(EquipmentExcel data) {
if (data == null) {
throwException("整行数据为空");
}
private List<EquipmentExcel> duplicateRemoval(List<EquipmentExcel> data) {
List<String> dbDeviceSource = baseMapper.getAllDeviceSource();
for (Iterator<EquipmentExcel> i = data.iterator(); i.hasNext(); ) {
EquipmentExcel equipmentExcel = i.next();
for (Iterator<String> j = dbDeviceSource.iterator(); j.hasNext(); ) {
String deviceSource = j.next();
if (equipmentExcel.getDeviceSource().equals(deviceSource)) {
i.remove();
}
}
}
return data;
}
private List<EquipmentExcel> duplicateRemovalExcel(List<EquipmentExcel> list) {
Set<EquipmentExcel> EquipmentExcelSet = new TreeSet<>((o1, o2) -> o1.getDeviceSource().compareTo(o2.getDeviceSource()));
EquipmentExcelSet.addAll(list);
return new ArrayList<>(EquipmentExcelSet);
}
}
|