我有一个 Cell 对象,如何获取该单元格的名称?
想要一个函数,例如:
String name = myCell.getName();
在Excel中,我已在名称框中对其进行命名,因此我不想获得“B4”,我想获得诸如“InterestRate”之类的名称。
找不到这样的方法,可以通过其他方式实现吗?
要查找定义为与一个单元格完全匹配的命名范围,您需要类似以下内容:
// 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();
}
}
}
请注意,这将返回适用于单元格的第一个命名范围,如果有多个并且您想要全部,则需要调整该代码以继续并返回一个列表
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)