As per my understanding, I am breaking the problem in two parts.
First:- need to know the cell range address in which we have data dynamically
Second:- re-define the cell range address.
Solution of First: get cell range dynamically that need to refer in range.
You can easily get the first cell (first row/first column of data) of the data (Say its A3) and last cell (last row/last column) (say its F9)
If you have any cell you can easily get its reference from
cell.getReference(); //will give you A3
Now we need to seperate A and 3 using simple string operation
char startCellColRef = cell.getReference().toString().charAt(0); // will Give you A
int startCellRowRef = cell.getReference().toString().charAt(1); // will give you 3
Using same way you can get the end index as well.
Solution of Second: Changing the cell references dynamically.
Name reference = wb.getName("NameReferenceInExcelSheet");
referenceString = sheetName+"!$"+startCellColRef+"$"+startCellRowRef+":$"+endCellColRef+"$"+endCellRowRef;
reference.setRefersToFormula(referenceString);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…