Fill combobox with XSSFRow

The name of the picture


Fill combobox with XSSFRow



I want to fill combobox with row numbers that has been taken from a excel sheet, the problem is I cannot loop row because of combobox.addItem() accepts only string. What other solution would solve this?
the error I got:


XSSFRow cannot be converted to String



My code:


int lastRow = sheets.getLastRowNum();
for (int i=0;i<lastRow;i++){
XSSFRow row = sheets.getRow(i);
chooseRowComboBox.addItem(row);
}




1 Answer
1



You can't get the whole row.. I think you want to get the rownumbers of the filled rows. So you should try something like:


public int getNonBlankRowCount(String path) throws IOException{
File excel = new File(path);
FileInputStream fis = new FileInputStream(excel);
XSSFWorkbook book = new XSSFWorkbook(fis);
XSSFSheet sheet = book.getSheetAt(1);
int rowCount = 0;
int index = book.getSheetIndex(sheet);
if(index==-1){
rowCount=-1;
return rowCount;
}
else{
sheet=book.getSheetAt(index);
Iterator<Row> rowIterator = sheet.rowIterator();
rowCount=0;
while(rowIterator.hasNext()){
Row row = (Row) rowIterator.next();
XSSFCell cell =(XSSFCell) row.getCell(0, Row.RETURN_BLANK_AS_NULL);
if(cell == null){
break;
}
rowCount++;
}
return rowCount;
}
}



This method will count ur filled rows. Now u just need to print the returned value into ur combobox like:


for(int i=0; i<getNonBlankRowCount("path_to_ur_excel"); i++){
chooseRowComboBox.addItem(Integer.toString(i)
}



If u want to get all rows, you just iterate threw all rows or set 0 < i < 1048577 (the max number of excel rows)






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Stripe::AuthenticationError No API key provided. Set your API key using “Stripe.api_key = ”

CRM reporting Extension - SSRS instance is blank

Keycloak server returning user_not_found error when user is already imported with LDAP