我的问题是我想要删除包含String JUNI的所有行。我成功地只在包含此字符串的一行上执行此操作我的代码我一直在使用:
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);
}
}
我只需要删除包含此字符串JUNI的Excel文件中的所有行。我可以这样做,请帮助我,自从过去3天以来我一直在努力〜提前致谢。
答案 0 :(得分:2)
试试这个。这将删除工作表的所有行,其中任何单元格包含" JUNI"作为其子串。
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;
}
}
}
}