Find a specific string in an Excel file using Apache POI API
15:18 20 Jun 2013

I'm new to Apache POI API for reading MS Office documents, but good at Java. I want to search a specific string from an Excel File. I'm doing in this way:

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("GET")){
                        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);
                        }
                        break;
                    }
                    else{
                        System.out.print("NOT FOUND");
                    }

                    break;
                }
            }
        }
    }
    catch(Exception e){
           JOptionPane.showMessageDialog(this,e.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
    }
}

All is working fine, except that I have a web generated log file. I have to search it for some strings and then I have to delete the line containing that string.

The problem is that it only works for the Excel files I'm creating by hand, but not for the computer generated file (automatic log file). Suppose that I have to search a string "POST", it will only find it in the file generated by me, but not in the file generated by the website. Is this an encoding problem?

Please suggest me a solution for this.

java excel apache-poi