我正在使用Apache POI库来读/写xlsx。在我原来的xlsx中,我有一个命名范围"XYZ“。
我想更新此名称范围内的单元格的值。我该怎么做呢?我这样做了:
XSSFName name = workbook.getName("XYZ");
String formula = name.getRefersToFormula();
System.out.println("Formula = " + formula);现在,我不知道如何获得此命名区域中单个单元格的句柄。
谁能告诉我我可以使用的正确的API?
Rgds Sapan
发布于 2015-10-17 13:50:02
这里有一个来自Busy Developers' Guide的示例,用于检索区域中的单元格。然后,您可以使用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.
}https://stackoverflow.com/questions/33183144
复制相似问题