I'm having a problem in excel while using Apache POI. I can read across rows, but sometimes I'm in a situation where I would like to read a particular column only.
So is it possible to read any particular column like only the 'A' column only or the column 'C' only.
I'm using the Java language for this.
To fetch a single row, we can use the getRow(int) method: Row row = sheet. getRow(2); The method returns the Row object – the high-level representation of a single row from the Excel file, or null if the row doesn't exist.
heikkim is right, here is some sample code adapted from some code I have:
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Row;
...
for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {
row = sheet.getRow(rowIndex);
if (row != null) {
Cell cell = row.getCell(colIndex);
if (cell != null) {
// Found column and there is value in the cell.
cellValueMaybeNull = cell.getStringCellValue();
// Do something with the cellValueMaybeNull here ...
// break; ???
}
}
}
For the colCount
use something like row.getPhysicalNumberOfCells()
Sheet sheet = workBook.getSheetAt(0); // Get Your Sheet.
for (Row row : sheet) { // For each Row.
Cell cell = row.getCell(0); // Get the Cell at the Index / Column you want.
}
My solution, a bit simpler code wise.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With