使用EasyExcel对excel数据进行相似度判断

打印 上一主题 下一主题

主题 1889|帖子 1889|积分 5667

  1. [/code][code]@Data
  2. public class ExeclDto {
  3.     /**
  4.      * execl表
  5.      */
  6.     private String filename;
  7.     /**
  8.      * 需要匹配的工作表名
  9.      */
  10.     private String name1;
  11.     /**
  12.      * 需要匹配的工作表名
  13.      */
  14.     private String name2;
  15. }
复制代码
  1. [/code] 
  2. [code]@SpringBootTest
  3. @Slf4j
  4. class CmmcysApplicationTests {
  5.     @Test
  6.     public void testReadExcel() throws IOException, InvalidFormatException {
  7.         // 读取的excel文件路径
  8.         String filename = "src/main/resources/static/xlsx/车型名称映射.xlsx";
  9.         String filename_new = "src/main/resources/static/xlsx/结果表.xlsx";
  10.         // 判断filename是否为空
  11.         if (!StringUtils.isEmpty(filename)) {
  12.             //读取excel
  13.             ExeclDto execlDto = new ExeclDto();
  14.             execlDto.setFilename(filename);
  15.             execlDto.setName1("A");
  16.             execlDto.setName2("B");
  17.             Map<String, String> map = readExcel(execlDto);
  18.             //如果map非空就写入
  19.             if (!map.isEmpty()) {
  20.                 List list = new ArrayList<>();
  21.                 for (Map.Entry<String, String> entry : map.entrySet()) {
  22.                     CarDto carDto = new CarDto();
  23.                     carDto.setName_a(entry.getKey());
  24.                     carDto.setName_b(entry.getValue());
  25.                     list.add(carDto);
  26.                     log.info(entry.getKey() + " ;" + entry.getValue());
  27.                 }
  28.                 EasyExcel.write(filename_new, CarDto.class).sheet("结果表").doWrite(list);
  29.             }else {
  30.                 System.out.println("文件没有该工作表,请重新检查上传");
  31.             }
  32.         } else {
  33.             System.out.println("文件为空,请重新上传");
  34.         }
  35.     }
  36.     public Map<String,String> readExcel(ExeclDto execlDto){
  37.         // 读取的excel文件路径
  38.         String filename = execlDto.getFilename();
  39.         // 读取excel
  40.         File file = new File(filename);
  41.         Workbook sheets = null;
  42.         try {
  43.             sheets = WorkbookFactory.create(file);
  44.         } catch (IOException e) {
  45.             e.printStackTrace();
  46.         } catch (InvalidFormatException e) {
  47.             e.printStackTrace();
  48.         }
  49.         Sheet sheetAt1 = sheets.getSheet(execlDto.getName1());
  50.         Sheet sheetAt2 = sheets.getSheet(execlDto.getName2());
  51.         //判断excel表中是否有该工作表
  52.         if (sheetAt1 !=null && sheetAt2 !=null) {
  53.             //采用LinkedHashMap保证数据的顺序性
  54.             Map<String, Double> map1 = new LinkedHashMap<>();
  55.             Map<String, String> map2 = new LinkedHashMap<>();
  56.             //循环工作表行
  57.             for (Row row1 : sheetAt1) {
  58.                 //获取工作表列值
  59.                 String stringCellValue = row1.getCell(0).getStringCellValue();
  60.                 //设置string类型初始阈值
  61.                 map1.put("bz", 0.0);
  62.                 //循环需要比较的工作表列值
  63.                 for (Row row2 : sheetAt2) {
  64.                     //获取工作表列值
  65.                     String stringCellValue1 = row2.getCell(0).getStringCellValue();
  66.                     //判断车辆款式,获取字符串位置
  67.                     if (stringCellValue.contains("款") && stringCellValue1.contains("款")) {
  68.                         int i = stringCellValue.indexOf("款");
  69.                         int b = stringCellValue1.indexOf("款");
  70.                         //进行款式截取并比较款式是否一致
  71.                         if (stringCellValue.substring(i - 4, i).equals(stringCellValue1.substring(b - 4, b))) {
  72.                             //采用String里面的方法进行相似度取值
  73.                             double similarity = StringUtils.getJaroWinklerDistance(stringCellValue, stringCellValue1);
  74.                             //进行相似度比较
  75.                             if (similarity > map1.get("bz")) {
  76.                                 //相似度高的替换低的
  77.                                 map1.put("bz", similarity);
  78.                                 //存放进map集合
  79.                                 map2.put(stringCellValue, stringCellValue1);
  80.                             }
  81.                         }
  82.                     }
  83.                 }
  84.             }
  85.             return map2;
  86.         }else {
  87.             return null;
  88.         }
  89.     }
  90. }
复制代码


 

 

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

数据人与超自然意识

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表