excel-获取最后一行导致的bug
sheet.getLastRowNum() 方法用于获取 Excel 工作表中最后一行的索引(行号),而不是数量,
static Map<String, String> map = new HashMap<>(); //第二次开发 找到两张表的差异 public static void main111(String[] args) throws Exception { System.out.println("----start----"); map.clear(); File file1 = new File("D:\\何旭光\\0824\\返修总表.xlsx"); File file2 = new File("D:\\何旭光\\0824\\需要反刷的海外ADU清单.xlsx"); find(file1, file2); find(file2, file1); Set set= map.keySet(); for(Object s:set){ System.out.println(" 没找到: "+s); } System.out.println("----end----"); } public static void find(File file1, File file2) throws Exception { FileInputStream stream1 = new FileInputStream(file1); XSSFWorkbook workbook1 = new XSSFWorkbook(stream1); FileInputStream stream2 = new FileInputStream(file2); XSSFWorkbook workbook2 = new XSSFWorkbook(stream2); XSSFSheet sheet2 = workbook2.getSheetAt(0); XSSFSheet sheet1 = workbook1.getSheetAt(0); int time = 0; int count = 0; int lastRowNum2 = sheet2.getLastRowNum(); for (int i = 0; i < lastRowNum2+1; i++) { XSSFRow row2 = sheet2.getRow(i); if (row2 == null) { continue; } XSSFCell cell = row2.getCell(1); if (cell == null) { continue; } String stringCellValue = cell.getStringCellValue(); int lastRowNum1 = sheet1.getLastRowNum(); Boolean flag = false; if(i==lastRowNum2-1){ System.out.println(stringCellValue+"最后"); } //第2个表 for (int j = 0; j < lastRowNum1+1; j++) { XSSFRow row1 = sheet1.getRow(j); if (row1 == null) { continue; } XSSFCell cell1 = row1.getCell(1); if (cell1 == null) { continue; } String stringCellValue1 = cell1.getStringCellValue(); if (stringCellValue1.equals(stringCellValue)) { flag = true; break; } } if (flag) { } else { map.put(stringCellValue, ""); } } workbook2.close(); stream2.close(); workbook1.close(); stream1.close(); System.out.println("end"); }