简体   繁体   English

得到细胞apache poi的名字

[英]Get name of cell apache poi

I have a Cell object, how can I get the name of that cell? 我有一个Cell对象,我怎样才能获得该单元格的名称?

Would like a function such as: 想要一个功能,如:

String name = myCell.getName();

In Excel I have named it in the name box, so I don't want to get 'B4', I would like to get the name such as "InterestRate". 在Excel中,我在名称框中命名,所以我不想得到'B4',我想得到像“InterestRate”这样的名字。

Can't find such a method, can I achieve it in some other way? 找不到这样的方法,我可以通过其他方式实现吗?

To find the named range which is defined to exactly match one cell, you'd want something like: 要查找定义为与一个单元格完全匹配的命名范围,您需要以下内容:

// 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();
      }
   }
}

Note that this will return the first named range that applies to a cell, if there's more than one and you wanted them all, you'd need to tweak that code to keep going and return a list 请注意,这将返回应用于单元格的第一个命名范围,如果有多个并且您想要它们全部,则需要调整该代码以继续并返回列表

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM