在Excel单元格设置自定义名,利用代码就可以获取单元格具体的位置
// Setup code
String cellName =
"TestName"
;
Workbook wb = getMyWorkbook();
// retrieve workbook
// Retrieve the named range
// Will be something like "$C$10,$D$12:$D$14";
int
namedCellIdx = wb.getNameIndex(cellName);
Name aNamedCell = wb.getNameAt(namedCellIdx);
// Retrieve the cell at the named range and test its contents
// Will get back one AreaReference for C10, and
// another for D12 to D14
AreaReference[] arefs = AreaReference.generateContiguous(aNamedCell.getRefersToFormula());
for
(
int
i=
0
; i<arefs.length; i++) {
// Only get the corners of the Area
// (use arefs[i].getAllReferencedCells() to get all cells)
CellReference[] crefs = arefs[i].getCells();
for
(
int
j=
0
; j<crefs.length; j++) {
// Check it turns into real stuff
Sheet s = wb.getSheet(crefs[j].getSheetName());
Row r = s.getRow(crefs[j].getRow());
Cell c = r.getCell(crefs[j].getCol());
// Do something with this corner cell
}
}