/**
* 校验Excel模板工具类
*/
public class ExcelCheckUtil {
/**
* 检验导入Excel的格式是否正确
*/
public static String checkExcelPattern(MultipartFile excel, Object obj) throws Exception {
if(excel == null || excel.isEmpty()){
throw new Exception("导入文件不能为空!");
}
//获取文件得拓展名
String extension = org.springframework.util.StringUtils.getFilenameExtension(excel.getOriginalFilename());
if (!Objects.equals(extension,"xlsx")){
throw new Exception("仅支持以.xlsx为拓展名的文件格式");
}
//用于拼接校验结果
StringBuilder builder = new StringBuilder();
//校验文件表头
BufferedInputStream header = new BufferedInputStream(excel.getInputStream());
//读取XLS和XLSX文件
Workbook wb = WorkbookFactory.create(header);
//获取注解当中的值
Map<String, List<String>> annotationValue = getAnnotationValue(obj);
List<String> annotationName = annotationValue.get("annotationName");
//获取到的实体注解名称顺序要与excel表头顺序保持一样
String[] columnName = annotationName.toArray(new String[]{});
Sheet sheet = wb.getSheetAt(0);
Row row = sheet.getRow(0);
if (row != null && row.getLastCellNum() >= columnName.length) {
int lastCellNum = row.getLastCellNum();
for (int idx = 0; idx < lastCellNum; idx++) {
String value = getCellValue(row.getCell(idx)).trim();
if (idx < columnName.length) {
if (StringUtils.isBlank(value) || !columnName[idx].equals(value)) {
builder.append("第" + (idx + 1) + "列表头应为" + columnName[idx]+"!");
}
} else {
if (idx == columnName.length) {
builder.append("导入文件只应该有:"+ columnName.length+"!");
}
}
}
} else {
builder.append("上传文件首行不能为空,且应与模板文件表头保持一致;");
}
if(builder.length()>0){
builder.append("请下载模板按照模板表头顺序进行上传!");
}
return builder.toString();
}
/**
* 获取Excel单元格的值
*/
private static String getCellValue(Cell cell) {
String cellValue;
// 以下是判断数据的类型
switch (cell.getCellType()) {
case Cell.CELL_TYPE_NUMERIC: // 数字
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
cellValue = sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue()));
} else {
DataFormatter dataFormatter = new DataFormatter();
cellValue = dataFormatter.formatCellValue(cell);
}
break;
case Cell.CELL_TYPE_STRING: // 字符串
cellValue = cell.getStringCellValue();
break;
case Cell.CELL_TYPE_BOOLEAN: // Boolean
cellValue = String.valueOf(cell.getBooleanCellValue());
break;
case Cell.CELL_TYPE_FORMULA: // 公式
cellValue = cell.getCellFormula();
break;
case Cell.CELL_TYPE_BLANK: // 空值
cellValue = "";
break;
case Cell.CELL_TYPE_ERROR: // 故障
cellValue = "非法字符";
break;
default:
cellValue = "未知类型";
break;
}
return cellValue;
}
/**
* 获取@ExcelField注解对象的字段名称以及注解的title值
* -annotationList:存储@ExcelField注解对象的字段名称
* -fieldList:存储@ExcelField注解的title值
*/
public static Map<String,List<String>> getAnnotationValue(Object obj) {
Map<String,List<String>>map=new HashMap<>();
List<String> fieldList=new ArrayList<>();
List<String> annotationList=new ArrayList<>();
Field[] fields = obj.getClass().getDeclaredFields();
for(int i = 0 ; i < fields.length ; i++) {
//设置是否允许访问,不是修改原来的访问权限修饰词。
fields[i].setAccessible(true);
ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
if (Objects.isNull(annotation)) continue;
String value = annotation.title();
/*for (String s : value) {
annotationList.add(s);
}*/
annotationList.add(value);
fieldList.add(fields[i].getName());
}
map.put("fieldName",fieldList);
map.put("annotationName",annotationList);
return map;
}
}