Skip to content
Advertisement

Using Apache POI how to read a specific excel column

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.

Advertisement

Answer

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()

Advertisement