fix:78298
This commit is contained in:
@@ -6,8 +6,21 @@ import com.jero.common.api.vo.Result;
|
||||
import com.jero.common.constant.CommonConstant;
|
||||
import com.jero.common.exception.JeroBootException;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.poi.ss.usermodel.Row;
|
||||
import org.apache.poi.ss.usermodel.Sheet;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.ss.usermodel.WorkbookFactory;
|
||||
import org.jeecgframework.poi.excel.annotation.Excel;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.InputStream;
|
||||
import java.io.PushbackInputStream;
|
||||
import java.lang.annotation.Annotation;
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
@@ -96,4 +109,74 @@ public class ImportExcelUtil {
|
||||
}
|
||||
return errorMessage;
|
||||
}
|
||||
|
||||
/**
|
||||
* 检查模板标题行模型是否匹配 不检查顺序(需要检查顺序的可自己优化)
|
||||
*
|
||||
* @param file 上传的excel 文件 对象
|
||||
* @param pojoClass 导入模型
|
||||
* @param titleRow 标题行下标
|
||||
* @param redundantNum 冗余数 默认为0
|
||||
* @return
|
||||
*/
|
||||
public static Boolean checkTemplateTitle(MultipartFile file, Class<?> pojoClass, Integer titleRow, int redundantNum) {
|
||||
//excel中标题行的所有标题
|
||||
List<String> excelList = new ArrayList<>();
|
||||
//注解中所有标题
|
||||
List<String> list = new ArrayList<>();
|
||||
Field[] fields = pojoClass.getDeclaredFields();
|
||||
for (Field field : fields) {
|
||||
Annotation excelAnn = field.getAnnotation(Excel.class);
|
||||
if (null != excelAnn) {
|
||||
Excel ec = (Excel) excelAnn;
|
||||
String name = ec.name();
|
||||
if (StringUtils.isNotBlank(name)) {
|
||||
list.add(name);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isEmpty(list)) {
|
||||
return true;
|
||||
}
|
||||
InputStream inputStream = null;
|
||||
try {
|
||||
inputStream = file.getInputStream();
|
||||
if (!(inputStream.markSupported())) {
|
||||
inputStream = new PushbackInputStream(inputStream, 8);
|
||||
}
|
||||
Workbook book = WorkbookFactory.create(inputStream);
|
||||
Sheet sheet = book.getSheetAt(0);
|
||||
Row row = sheet.getRow(titleRow);
|
||||
for (int i = 0; i < row.getPhysicalNumberOfCells(); i++) {
|
||||
String value = row.getCell(i).getStringCellValue();
|
||||
if (StringUtils.isNotBlank(value)) {
|
||||
excelList.add(value);
|
||||
}
|
||||
}
|
||||
book.close();
|
||||
} catch (Exception e) {
|
||||
throw new JeroBootException("excel表头错误");
|
||||
}
|
||||
if (CollectionUtils.isEmpty(excelList)) {
|
||||
return false;
|
||||
}
|
||||
boolean flag = true;
|
||||
flag = excelContainsList(redundantNum, excelList, list, flag);
|
||||
return flag;
|
||||
}
|
||||
private static boolean excelContainsList(int redundantNum, List<String> excelList, List<String> list,boolean flag) {
|
||||
if(excelList.size() != list.size()){
|
||||
flag = false;
|
||||
}
|
||||
int i = 0;
|
||||
for (String name : list) {
|
||||
if (!excelList.contains(name)) {
|
||||
i++;
|
||||
}
|
||||
}
|
||||
if (i > redundantNum) {
|
||||
flag = false;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user