Get name of cell apache poi

浪尽此生 提交于 2020-04-10 21:09:58

问题


I have a Cell object, how can I get the name of that 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".

Can't find such a method, can I achieve it in some other way?


回答1:


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



来源:https://stackoverflow.com/questions/26649598/get-name-of-cell-apache-poi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!