- [/code][code]@Data
- public class ExeclDto {
- /**
- * execl表
- */
- private String filename;
- /**
- * 需要匹配的工作表名
- */
- private String name1;
- /**
- * 需要匹配的工作表名
- */
- private String name2;
- }
复制代码- [/code]
- [code]@SpringBootTest
- @Slf4j
- class CmmcysApplicationTests {
- @Test
- public void testReadExcel() throws IOException, InvalidFormatException {
- // 读取的excel文件路径
- String filename = "src/main/resources/static/xlsx/车型名称映射.xlsx";
- String filename_new = "src/main/resources/static/xlsx/结果表.xlsx";
- // 判断filename是否为空
- if (!StringUtils.isEmpty(filename)) {
- //读取excel
- ExeclDto execlDto = new ExeclDto();
- execlDto.setFilename(filename);
- execlDto.setName1("A");
- execlDto.setName2("B");
- Map<String, String> map = readExcel(execlDto);
- //如果map非空就写入
- if (!map.isEmpty()) {
- List list = new ArrayList<>();
- for (Map.Entry<String, String> entry : map.entrySet()) {
- CarDto carDto = new CarDto();
- carDto.setName_a(entry.getKey());
- carDto.setName_b(entry.getValue());
- list.add(carDto);
- log.info(entry.getKey() + " ;" + entry.getValue());
- }
- EasyExcel.write(filename_new, CarDto.class).sheet("结果表").doWrite(list);
- }else {
- System.out.println("文件没有该工作表,请重新检查上传");
- }
- } else {
- System.out.println("文件为空,请重新上传");
- }
- }
- public Map<String,String> readExcel(ExeclDto execlDto){
- // 读取的excel文件路径
- String filename = execlDto.getFilename();
- // 读取excel
- File file = new File(filename);
- Workbook sheets = null;
- try {
- sheets = WorkbookFactory.create(file);
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- }
- Sheet sheetAt1 = sheets.getSheet(execlDto.getName1());
- Sheet sheetAt2 = sheets.getSheet(execlDto.getName2());
- //判断excel表中是否有该工作表
- if (sheetAt1 !=null && sheetAt2 !=null) {
- //采用LinkedHashMap保证数据的顺序性
- Map<String, Double> map1 = new LinkedHashMap<>();
- Map<String, String> map2 = new LinkedHashMap<>();
- //循环工作表行
- for (Row row1 : sheetAt1) {
- //获取工作表列值
- String stringCellValue = row1.getCell(0).getStringCellValue();
- //设置string类型初始阈值
- map1.put("bz", 0.0);
- //循环需要比较的工作表列值
- for (Row row2 : sheetAt2) {
- //获取工作表列值
- String stringCellValue1 = row2.getCell(0).getStringCellValue();
- //判断车辆款式,获取字符串位置
- if (stringCellValue.contains("款") && stringCellValue1.contains("款")) {
- int i = stringCellValue.indexOf("款");
- int b = stringCellValue1.indexOf("款");
- //进行款式截取并比较款式是否一致
- if (stringCellValue.substring(i - 4, i).equals(stringCellValue1.substring(b - 4, b))) {
- //采用String里面的方法进行相似度取值
- double similarity = StringUtils.getJaroWinklerDistance(stringCellValue, stringCellValue1);
- //进行相似度比较
- if (similarity > map1.get("bz")) {
- //相似度高的替换低的
- map1.put("bz", similarity);
- //存放进map集合
- map2.put(stringCellValue, stringCellValue1);
- }
- }
- }
- }
- }
- return map2;
- }else {
- return null;
- }
- }
- }
复制代码


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