Apache POI:更新指定范围内的单元格



我使用Apache POI库读取/写入xlsx。在我的原始xlsx中,我有一个名称范围"XYZ"。

我想更新这个名称范围内的单元格值。我该怎么做呢?我这样做了:

XSSFName name = workbook.getName("XYZ");
String formula = name.getRefersToFormula();
System.out.println("Formula = " + formula);

现在,我不知道如何在这个命名范围内获得单个单元格的句柄。

谁能告诉我正确的API,我可以使用?

祝好苏木木材

有一个来自忙碌开发人员指南的示例,用于检索区域中的单元格。然后你可以使用Cell.setCellValue()来更新。

// setup code
String cname = "TestName";
Workbook wb = getMyWorkbook(); // retrieve workbook
// retrieve the named range
int namedCellIdx = wb.getNameIndex(cname);
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 = s.getRow(crefs[i].getRow());
    Cell c = r.getCell(crefs[i].getCol());
    // extract the cell contents based on cell type etc.
}

最新更新