servlets - how to get last column value of respective row of excel sheet in java using Apache POI -


i have following excel sheet

id     jobid     name      dept        add       salary 101      41      bob     editing    new york     $ 5000  

i passing id,jobid request parameter servlet $.ajax() method. want salary cell data of excel sheet matching request parameters i.e id , jobid excel sheet contents , insert other table. how this?

here code...

   fileinputstream file = new fileinputstream(filetoberead);    workbook = new hssfworkbook(file);    int jobid = integer.parseint(request.getparameter("jobid"));            if (id == 101) {              // first sheet                 hssfsheet sheet = workbook.getsheetat(0);                iterator<row> rowiterator = sheet.iterator();                statement = conn.createstatement() ;                resultset resultset = statement.executequery("select job_id,job,name, deptname emp,department department.job_id=" + jobid + " , emp.id=" + id + " ");                  while (resultset.next()) {                      //how match database result excel records , save other table                    }//resultset.next() ends here...              resultset.close();         } 

i try this......? e.g gives error trying match id=101 , add=new york e.g , want salary data. if use if(data[i][j].equalsignorecase("101")) condition works want match both id , address i.e. if(data[i][j].equalsignorecase("101") && data[i][j+5].equalsignorecase("new york")) gives error

    int rownum = sheet.getlastrownum() + 1;     int colnum = sheet.getrow(0).getlastcellnum();     string[][] data = new string[rownum][colnum];    // system.out.println("row no :"+rownum +" \ncol no:"+colnum);      (int = 1; < rownum; i++) {         row row = sheet.getrow(i);         (int j = 0; j < colnum; j++) {             cell cell = row.getcell(j);             string value = null;             double price;            // int type = cell.getcelltype();              value = cell.getstringcellvalue();                      data[i][j] = value;                     if(data[i][j].equalsignorecase("101") && data[i][j+4].equalsignorecase("new york"))                         {                             cell lastcellinrow = row.getcell(row.getlastcellnum() - 1);                             system.out.println(lastcellinrow.getstringcellvalue());                      }          }     } 

if you're trying access last cell in row, , have row, try following:

cell lastcellinrow = row.getcell(row.getlastcellnum() - 1); // -1 because #s 0-based 

edit address updated question , comments below. code should you're looking without going through rigmarole of converting entire spreadsheet 2d array, frankly terribly inefficient when have access data right @ fingertips.

for (int = 1; < rownum; i++) {   row row = sheet.getrow(i);   cell cell1 = row.getcell(0);   cell cell2 = row.getcell(4);   string id = cell1.getstringcellvalue();   string city = cell2.getstringcellvalue();    if(id.equalsignorecase("101") && city.equalsignorecase("new york")) {     cell lastcellinrow = row.getcell(row.getlastcellnum() - 1);     system.out.println(lastcellinrow.getstringcellvalue());   } } 

Comments

Popular posts from this blog

image - ClassNotFoundException when add a prebuilt apk into system.img in android -

I need to import mysql 5.1 to 5.5? -

Java, Hibernate, MySQL - store UTC date-time -