apache poi - How to do cell iteration of excel in java -
i having excel 2 rows , 5 columns. entered code manually take values 1st row. how can iterate process?
below code 1st row in excel. 2nd row on, dont know how do... want iterate 1 row after another.
workbook workbook = workbook.getworkbook(new file( "\\c:\\users\\a-4935\\desktop\\datapool_ca.xls")); sheet sheet = workbook.getsheet("sheet1"); system.out.println("reached sheet"); cell = sheet.getcell(2,1); cell b = sheet.getcell(3,1); cell c = sheet.getcell(4,1); cell d = sheet.getcell(5,1); cell e = sheet.getcell(6,1); cell f = sheet.getcell(7,1); cell g = sheet.getcell(8,1); cell h = sheet.getcell(9,1); cell = sheet.getcell(10,1); string uid = a.getcontents(); string deptfromdat = b.getcontents(); string depttodate = c.getcontents(); string dept1 = d.getcontents(); string arrival1 = e.getcontents(); string eihon1 = f.getcontents(); string branchcode1 = g.getcontents(); string usertype1 = h.getcontents(); string sessionid1 = i.getcontents();
use code below iterate on rows of datasheet:
sheet sheet = workbook.getsheet("sheet1"); (row row : sheet) { (cell cell : row) { //your logic } }
or, alternatively, use following code:
sheet sheet = workbook.getsheet("sheet1"); (int = 0; < 2; i++) { row row = sheet.getrow(i); if(row == null) { //do empty row continue; } (int j = 0; j < 5; j++) { cell cell = row.getcell(j); if(cell == null) { //do empty cell continue; } //your logic } }