首页 > 解决方案 > 如何从工作簿之一中读取不唯一的命名范围

问题描述

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

我尝试将工作簿作为一个整体,无法在工作表级别设置范围

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.
}

标签: javaapache-poi

解决方案


推荐阅读