How to get the Cell value of A1(Cell Address) using apache poi 3.6
CellReference cr = new CellReference("A1");
row = mySheet.getRow(cr.getRow());
cell = row.getCell(cr.getCol());
See Quick Guide for more samples.
There's a cellReference function
E.g.
CellReference cellReference = new CellReference("B3");
(taken from the example here)
(Code on Kotlin)
You have 2 options to specify cell address:
Option 1: by CellReference
(which can contain reference to the sheet name):
val cellReference = CellReference("SheetName!C11")
val sheet = workbook.getSheet(cellReference.sheetName)
val row = sheet.getRow(cellReference.row)
val cell: Cell = row.getCell(cellReference.col.toInt())
Option 2: by CellAddress
(which can only contain reference on the cell within a sheet):
val sheetName = "SheetName"
val cellAddress = CellAddress("C11")
val sheet = workbook.getSheet(sheetName)
val row = sheet.getRow(cellAddress.row)
val cell: Cell = row.getCell(cellAddress.column)
Unfortunately code can't be simpler because both Workbook
and Sheet
have no methods which return cell by the CellReference
or CellAddress
.
Note: getCell()
overload with MissingCellPolicy
parameter allows to write code in more safe manner:
val cell: Cell? = row.getCell(cellAddress.column, Row.MissingCellPolicy.RETURN_BLANK_AS_NULL)
return cell ?: throw UnexpectedExcelStructureException("Unable to find $cellAddress cell on the $sheetName sheet or cell is empty")