Reputation: 37
How can i get a specific cell value from my excel xlsx sheet ? For example i want to get all values from the cell 6 and 8. Here's what i done - i get the whole row
public void readExcelFile() {
try {
InputStream ExcelFileToRead = new FileInputStream("D:\\20161111_Gesamtliste_aller_Anschreiben.xlsx");
XSSFWorkbook wb = new XSSFWorkbook(ExcelFileToRead);
XSSFSheet sheet = wb.getSheetAt(0);
XSSFRow row;
XSSFCell cell;
Iterator rows = sheet.rowIterator();
int read = 0;
while (rows.hasNext()) {
read++;
row = (XSSFRow) rows.next();
Iterator cells = row.cellIterator();
while (cells.hasNext()) {
cell = (XSSFCell) cells.next();
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
System.out.print(cell.getStringCellValue() + " ");
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue() + " ");
} else {
}
}
System.out.println();
}
System.out.println(read);
} catch (Exception ex) {
ex.printStackTrace();
}
}
Upvotes: 0
Views: 445
Reputation: 385
Row row = (XSSFRow) rows.next();
int rowcellnum = row.getLastCellNum();
for (int cn = 0; cn < rowcellnum; cn++) {
String cellVal = "";
Cell cell = row.getCell(cn);
if (cell == null) {
//HANDLE NULL
}
if (cell.getCellType() == XSSFCell.CELL_TYPE_STRING) {
System.out.print(cell.getStringCellValue() + " ");
} else if (cell.getCellType() == XSSFCell.CELL_TYPE_NUMERIC) {
System.out.print(cell.getNumericCellValue() + " ");
} else {
}
}
Based on index i.e cn you can access specific cell.
Upvotes: 2