Saturday 15 May 2010

java - Find a Specific String in Excel File using Apache POI API -


Hi, I'm new to Apache POI API for reading MS Office documents, but Java is somehow good. I just want to find specific strings from excel file! I am doing this like this:
  Exclusive zero process excel file (file f_path) throws exception {file path = f_path; {FileInputStream = Try New FileInputStream (Path); HSSFWorkbook wb = New HSSFWorkbook (is); HSSFSheet Sheet = wb.getSheetAt (0); Int rowcount_post = 0; Int rowcount_304 = 0; (Line row: sheet) {for (cell cell: line) {if (cell.getCellType () == cell.cell_TYPE_STRING) {if (cell.getRichStringCellValue (). GetString (.) Trim (.) Equals ("GET" )) {Rowcount_post = row.getRowNum (); HSSFRow removal = right = sheet.lite (Panditikota_post); If (by removing! = Zero) {sheet.removeRow (for deletion); } Try (FileOutputStream fileOut = New FileOutputStream ("C: /juni.xls")) {wb.write (fileOut); } break; } And {System.out.print ("not found"); } break; }}}} Hold (exception e) {JOptionPane.showMessageDialog (this, e.getMessage (), "error", JOptionPane.ERROR_MESSAGE); }}   

All are working fine! Except that I have a web generated log file so I have to find it for some strings and then I have to delete that string line. The problem is that it only works for Excel files, which is composed by my hand, but not for the computer generated (automated log file) file. Suppose I want to find the string "POST", it will only be found in the file I created, but not in the file generated by the website. Is this an encoding problem which means that it is encoded or whatever! Please let me know this thanks in advance! (Sorry for the bad english I'm trying to resolve since the last 10 hours)

There are two bugs I have seen in your code first, whenever you disable a file every time you want to remove a row which is disabled. Second, you only find the first string cell in the row, and if the first string- If the cell is not correct, you can break if your self generated file Saves numbers or empty cells as strings (possible), so this will create an Axl file with your hand, and the difference between the file you have created with the script will be different. I would suggest removing another break statement. To clean the code, I would like to completely rewrite if the block is as follows:

  if (cell.getCellType () == cell.cell_TYPE_STRING & amp; cell ; Remove stringkelvalue (). Equals ("GET")) {sheet.removeRow (line); break; } Else {System.out.print ("Not Found!"); }   

Due to short circuiting, if statement works if you are new to Apache Poole, it will save you a lot of time :)

We remove rows After, we should write the file. For the block, keep following the whole:

  Try (FileOutputStream fileOut = New FileOutputStream ("C: /juni.xls")) {wb.write (fileOut); }   

I'm assuming that sheet. Revoke Cry (Cry) will increase for a loop. If it is not, you may need to fix it or work your code in the loop for a while. Hint: While ((line = sheet. Gateway (i))! = Null) {}

No comments:

Post a Comment