poi 两个excel对比,输出到新的excel

  List<String> ips1 = new ArrayList<>();
        List<String> ips2 = new ArrayList<>();

        File file1 = new File("C:\\Users\\Administrator\\Desktop\\20241224.xlsx");
        File file2 = new File("C:\\Users\\Administrator\\Desktop\\ip.xlsx");
        InputStream inputStream1 = new FileInputStream(file1);
        InputStream inputStream2 = new FileInputStream(file2);

        //===============1
        Workbook wb1 = WorkbookFactory.create(inputStream1);
        Sheet sheet01 = wb1.getSheetAt(0);
        // 得到总行数
        int rowNum01 = sheet01.getLastRowNum();
        for (int i = 0; i <= rowNum01; i++) {
            Row row01 = sheet01.getRow(i);
            String ip = row01.getCell(6).getRichStringCellValue().toString().replace(" ", "");
            ips1.add(ip);
        }
        //======================2
        Workbook wb2 = WorkbookFactory.create(inputStream2);
        Sheet sheet02 = wb2.getSheetAt(0);
        // 得到总行数
        int rowNum02 = sheet02.getLastRowNum();
        for (int i = 0; i <= rowNum02; i++) {
            Row row02 = sheet02.getRow(i);
            String ip = row02.getCell(0).getRichStringCellValue().toString().replace(" ", "");
            ips2.add(ip);
        }

        Workbook workBook = new XSSFWorkbook();
        Sheet sheet = workBook.createSheet();
        for (int i = 0; i < ips1.size(); i++) {
            Row row = sheet.createRow(i);
            row.createCell(0).setCellValue(ips1.get(i));
            if(ips2.contains(ips1.get(i))){
                row.createCell(1).setCellValue("是");
            }else {
                row.createCell(1).setCellValue("否");
            }
        }
        try (FileOutputStream out = new FileOutputStream(new File("C:\\Users\\Administrator\\Desktop\\打标.xlsx"))){
            workBook.write(out);
        }

  

posted @ 2025-01-14 10:52  我没有出家  阅读(13)  评论(0)    收藏  举报