1. /**
    2. * 校验Excel模板工具类
    3. */
    4. public class ExcelCheckUtil {
    5. /**
    6. * 检验导入Excel的格式是否正确
    7. */
    8. public static String checkExcelPattern(MultipartFile excel, Object obj) throws Exception {
    9. if(excel == null || excel.isEmpty()){
    10. throw new Exception("导入文件不能为空!");
    11. }
    12. //获取文件得拓展名
    13. String extension = org.springframework.util.StringUtils.getFilenameExtension(excel.getOriginalFilename());
    14. if (!Objects.equals(extension,"xlsx")){
    15. throw new Exception("仅支持以.xlsx为拓展名的文件格式");
    16. }
    17. //用于拼接校验结果
    18. StringBuilder builder = new StringBuilder();
    19. //校验文件表头
    20. BufferedInputStream header = new BufferedInputStream(excel.getInputStream());
    21. //读取XLS和XLSX文件
    22. Workbook wb = WorkbookFactory.create(header);
    23. //获取注解当中的值
    24. Map<String, List<String>> annotationValue = getAnnotationValue(obj);
    25. List<String> annotationName = annotationValue.get("annotationName");
    26. //获取到的实体注解名称顺序要与excel表头顺序保持一样
    27. String[] columnName = annotationName.toArray(new String[]{});
    28. Sheet sheet = wb.getSheetAt(0);
    29. Row row = sheet.getRow(0);
    30. if (row != null && row.getLastCellNum() >= columnName.length) {
    31. int lastCellNum = row.getLastCellNum();
    32. for (int idx = 0; idx < lastCellNum; idx++) {
    33. String value = getCellValue(row.getCell(idx)).trim();
    34. if (idx < columnName.length) {
    35. if (StringUtils.isBlank(value) || !columnName[idx].equals(value)) {
    36. builder.append("第" + (idx + 1) + "列表头应为" + columnName[idx]+"!");
    37. }
    38. } else {
    39. if (idx == columnName.length) {
    40. builder.append("导入文件只应该有:"+ columnName.length+"!");
    41. }
    42. }
    43. }
    44. } else {
    45. builder.append("上传文件首行不能为空,且应与模板文件表头保持一致;");
    46. }
    47. if(builder.length()>0){
    48. builder.append("请下载模板按照模板表头顺序进行上传!");
    49. }
    50. return builder.toString();
    51. }
    52. /**
    53. * 获取Excel单元格的值
    54. */
    55. private static String getCellValue(Cell cell) {
    56. String cellValue;
    57. // 以下是判断数据的类型
    58. switch (cell.getCellType()) {
    59. case Cell.CELL_TYPE_NUMERIC: // 数字
    60. if (DateUtil.isCellDateFormatted(cell)) {
    61. SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    62. cellValue = sdf.format(DateUtil.getJavaDate(cell.getNumericCellValue()));
    63. } else {
    64. DataFormatter dataFormatter = new DataFormatter();
    65. cellValue = dataFormatter.formatCellValue(cell);
    66. }
    67. break;
    68. case Cell.CELL_TYPE_STRING: // 字符串
    69. cellValue = cell.getStringCellValue();
    70. break;
    71. case Cell.CELL_TYPE_BOOLEAN: // Boolean
    72. cellValue = String.valueOf(cell.getBooleanCellValue());
    73. break;
    74. case Cell.CELL_TYPE_FORMULA: // 公式
    75. cellValue = cell.getCellFormula();
    76. break;
    77. case Cell.CELL_TYPE_BLANK: // 空值
    78. cellValue = "";
    79. break;
    80. case Cell.CELL_TYPE_ERROR: // 故障
    81. cellValue = "非法字符";
    82. break;
    83. default:
    84. cellValue = "未知类型";
    85. break;
    86. }
    87. return cellValue;
    88. }
    89. /**
    90. * 获取@ExcelField注解对象的字段名称以及注解的title值
    91. * -annotationList:存储@ExcelField注解对象的字段名称
    92. * -fieldList:存储@ExcelField注解的title值
    93. */
    94. public static Map<String,List<String>> getAnnotationValue(Object obj) {
    95. Map<String,List<String>>map=new HashMap<>();
    96. List<String> fieldList=new ArrayList<>();
    97. List<String> annotationList=new ArrayList<>();
    98. Field[] fields = obj.getClass().getDeclaredFields();
    99. for(int i = 0 ; i < fields.length ; i++) {
    100. //设置是否允许访问,不是修改原来的访问权限修饰词。
    101. fields[i].setAccessible(true);
    102. ExcelField annotation = fields[i].getAnnotation(ExcelField.class);
    103. if (Objects.isNull(annotation)) continue;
    104. String value = annotation.title();
    105. /*for (String s : value) {
    106. annotationList.add(s);
    107. }*/
    108. annotationList.add(value);
    109. fieldList.add(fields[i].getName());
    110. }
    111. map.put("fieldName",fieldList);
    112. map.put("annotationName",annotationList);
    113. return map;
    114. }
    115. }