i have an excel sheet. i am parsing it using java POI.
i need to parse rows 6 - 15 with help of an for loop.
at 16th row, the first cell is empty and the third and fourth row have the values in it. so what should be the condition to break out of loop when the counter reaches the 16th row?
Note: Iteration values will change for different excel sheet. therefore i need a general for-loop break condition.
for( rowNo = 5; ; rowNo++){
if(sheet.getRow(rowNo).getCell(0).getCellType() == Cell.CELL_TYPE_BLANK || sheet.getRow(rowNo) == null || sheet.getRow(rowNo).getCell(0).getStringCellValue() == ""){
break;
}
for(int cellNo = 0; cellNo < 5 ; cellNo++){
switch(cellNo){
case 0: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_STRING){
productId = formatter.formatCellValue(sheet.getRow(rowNo).getCell(cellNo));
System.out.println(productId);
}
break;
case 1: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_STRING){
productName = formatter.formatCellValue(sheet.getRow(rowNo).getCell(cellNo));
System.out.println(productName);
}
break;
case 2: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_NUMERIC){
quantity = (int) sheet.getRow(rowNo).getCell(cellNo).getNumericCellValue();
System.out.println(quantity);
}
break;
case 3: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_NUMERIC){
price = (int) sheet.getRow(rowNo).getCell(cellNo).getNumericCellValue();
System.out.println(price);
}
break;
case 4: if(sheet.getRow(rowNo).getCell(cellNo).getCellType() == Cell.CELL_TYPE_NUMERIC){
lineTotal = (int) sheet.getRow(rowNo).getCell(cellNo).getNumericCellValue();
System.out.println(lineTotal);
}
break;
}
}
product.setProductName(productName);
product.setQuantity(quantity);
product.setPrice(price);
product.setLineTotal(lineTotal);
productDetails.put(productId, product);
}
//need to reach here after first loop breaks, instead reaches catch throws NPE. no reason why the NPE. it just throws NPE
totalPrice = (int)sheet.getRow(rowNo).getCell(4).getNumericCellValue();
invoice.setInvoiceDate(invoiceDate);
invoice.setInvoiceNumber(invoiceNo);
invoice.setProductDetails(product);
invoice.setProductId(productId);
invoice.setRetailerId(retailerNo);
invoice.setProductMap(productDetails);
invoice.setTotalPrice(totalPrice);
return invoice;
}catch(Exception e){
e.printStackTrace();
e.getMessage();
}