我有一个单元格(Cell)对象,如何获取该单元格的名称?
需要一个类似这样的函数:
String name = myCell.getName();
在Excel中,我已经在名称框中命名了它,所以我不想得到“B4”,我想得到名称,例如“InterestRate”。找不到这样的方法,我能用其他方法实现吗?
我有一个单元格(Cell)对象,如何获取该单元格的名称?
需要一个类似这样的函数:
String name = myCell.getName();
要查找与单元格完全匹配的命名区域,您需要类似以下内容:
// Get the cell we want to find - A1 for this case
Workbook wb = WorkbookFactory.create("input.xlsx");
int sheetIndex = 0;
Sheet s = wb.getSheetAt(sheetIndex);
Cell wanted = s.getRow(0).getCell(0);
String wantedRef = (new CellReference(wanted)).formatAsString();
// Check all the named range
for (int nn=0; nn<wb.getNumberOfNames(); nn++) {
Name n = wb.getNameAt(nn);
if (n.getSheetIndex() == -1 || n.getSheetIndex() == sheetIndex) {
if (n.getRefersToFormula().equals(wantedRef)) {
// Found it!
return name.getNameName();
}
}
}