I have Excel Cell Address like A1,A2. So, how to access this cell programatically using poi3.6
another way is
row=mySheet.getRow();
cell=row.getCell();
But i have the address in the format of A1 ... so, how do I access those cell programatically
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)
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] ar)
{
// char a = 'A', b = 'B';
//string[] ab = new string[100];
//ab[0]= "A1";
int k = 0, h = 0; ;
string ab = "B1";
int l;
l = Convert.ToInt32(ab.Length.To String());
for (int i = 0; i < l; i++)
{
if(!char.Is Digit(ab[i]))
{
for (int j = 65; j <= 90; j++)
{
if(Convert.ToInt32(ab[i])==j)
{
break;
}
else
{
k = 1-k;
}
}
}
else
{
h = (Convert.ToInt32(ab[i]))-49;
}
}
Console.Write Line(l);
Console.Write Line(h+""+k);
}
}}
replace variable 'ab' as your excel cell address
(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")