我一直在尝试开发一个代码,该代码可以删除包含特定字符串“ POST”的多行。我一直在这样做:
private void processExcelFile(File f_path){
File path=f_path;
HSSFWorkbook wb = null;
try{
FileInputStream is=new FileInputStream(path);
wb= new HSSFWorkbook(is);
HSSFSheet sheet = wb.getSheetAt(0);
int j = 0;
for(Row row: sheet){
for(Cell cell : row){
count++;
if (cell.getCellType() == Cell.CELL_TYPE_STRING){
if (cell.getRichStringCellValue().getString().trim().contains("POST")){
rowcount_post=row.getRowNum();
System.out.print("ROW COUNT= "+rowcount_post);
HSSFRow removingRow = sheet.getRow(rowcount_post);
if (removingRow != null) {
sheet.removeRow(removingRow);
}
}
}
}
}
} catch(Exception e){
JOptionPane.showMessageDialog(this,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
}
try{
FileOutputStream fileOut = new FileOutputStream("C:/juni.xls");
wb.write(fileOut);
} catch(Exception e) {
JOptionPane.showMessageDialog(this,e.getMessage(),"SAVE Error",JOptionPane.ERROR_MESSAGE);
}
}
事实上,它一次只删除一行。是否可以删除包含字符串POST的所有行?
最佳答案
为什么一次删除一个有问题?似乎效率不高...
无论如何,我很确定您仍然必须遍历每个单元格以查看其是否包含“ POST”。
如果您在一次删除一行时遇到了问题,则可以将要删除的行的索引保存在数组中;然后使用该数组删除保存的行。