Reputation: 155
How to get height and width of excel sheet named range area from HSSFWorkbook POI API
.
I got one reference from google but not able to get height and width of named index area of excel sheet.
Thanks in advance.
Yes it's named range, and want height and width of cells aquired by named range. below code having "print_area" named range and want to height and width of cells.
int namedCellIdx = workbook.getNameIndex("Print_Area"); HSSFName aNamedCell = workbook.getNameAt(namedCellIdx);
// retrieve the cell at the named range and test its contents
String a = aNamedCell.getReference();
AreaReference aref = new AreaReference(aNamedCell.getRefersToFormula());
CellReference[] crefs = aref.getAllReferencedCells();
for (int i=0; i<crefs.length; i++) {
Sheet s = workbook.getSheet(crefs[i].getSheetName());
Row r = sheet.getRow(crefs[i].getRow());
System.out.println(r.getHeight());
System.out.println(sheet.getColumnWidth(crefs[i].getCol()));;
//here want height and width of "Print_area" cells
Cell c = r.getCell(crefs[i].getCol());
System.out.println(c.getStringCellValue());
// extract the cell contents based on cell type etc.
}
Upvotes: 0
Views: 2223
Reputation: 86774
Read the API docs at http://poi.apache.org/apidocs/index.html
Specifically, look at org.apache.poi.ss.util.AreaReference
, from which you can get the first and last cell references and determine the size of the range.
However, you have to cope with a number of special cases, such as areas that are an entire column or entire row, or even non-contiguous.
Upvotes: 1