如何从工作簿之一读取不唯一的命名范围

时间:2019-04-29 08:58:55

标签: java apache-poi

如何从工作簿中的工作表之一读取命名范围。多张工作簿中存在相同名称的命名范围。

我尝试了整个工作簿,但无法在工作表级别设置范围

String cname = "TestName";
Workbook wb = getMyWorkbook(); // retrieve workbook

// retrieve the named range
int namedCellIdx = wb.getNameIndex(cellName);
Name aNamedCell = wb.getNameAt(namedCellIdx);

// retrieve the cell at the named range and test its contents
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
    Sheet s = wb.getSheet(crefs[i].getSheetName());
    Row r = sheet.getRow(crefs[i].getRow());
    Cell c = r.getCell(crefs[i].getCol());
    // extract the cell contents based on cell type etc.
}

0 个答案:

没有答案