删除包含stringExcel Apache POI API的行

我的问题是,我想删除所有包含stringJUNI的行。 我成功做到这一点,只有一行包含这个string我的代码,我一直在使用的是:

private void processExcelFile(File f_path) throws Exception{ File path=f_path; try{ FileInputStream is=new FileInputStream(path); HSSFWorkbook wb = new HSSFWorkbook(is); HSSFSheet sheet = wb.getSheetAt(0); int rowcount_post=0; int rowcount_304=0; for(Row row: sheet){ for(Cell cell : row){ if (cell.getCellType() == Cell.CELL_TYPE_STRING){ if (cell.getRichStringCellValue().getString().trim().equals("JUNI")){ rowcount_post=row.getRowNum(); HSSFRow removingRow = sheet.getRow(rowcount_post); if (removingRow != null) { sheet.removeRow(removingRow); } try (FileOutputStream fileOut = new FileOutputStream("C:/juni.xls")) { wb.write(fileOut); } } else{ System.out.print("NOT FOUND"); } } } } } catch(Exception e){ JOptionPane.showMessageDialog(this,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE); } } 

我只需要删除我的Excel文件中包含这个string的所有行JUNI.How我可以这样做,请帮助我我一直在努力,我最好的,因为最近3天〜在此先感谢。

尝试这个。 这将删除表单中的所有行,其中任何单元格包含“JUNI”作为其子string。

  public static void test(XSSFSheet sheet){ XSSFRow Row = null; XSSFCell Cell = null; int LastRowNum = sheet.getLastRowNum(); for(int RowNum= 0;RowNum<LastRowNum-1;RowNum++){ Row=sheet.getRow(RowNum); for(int CellNum = 0; CellNum<Row.getLastCellNum();CellNum++){ Cell = Row.getCell(CellNum); String TextInCell=Cell.toString(); if(TextInCell.contains("JUNI")){ sheet.shiftRows(RowNum+1, LastRowNum, -1); LastRowNum--; RowNum--; break; } } } }