define dynamic cell range

血红的双手。 提交于 2020-01-06 19:29:42

问题


I am using APACHE POI for conditional formatting. I get txt file as input and i convert it into worksheet and then perform the conditional formatting. Now, the first column and header are text on which I wont do any formatting. I have to do conditional formatting on rest of the cells, like B2:I10(but it would keep changing) How can i define the cell range dynamically.

Currently, I have

CellRangeAddress[] my_data_range = {CellRangeAddress.valueOf("B2:I10")};
my_cond_format_layer.addConditionalFormatting(my_data_range,my_rule1)

how would i define my_data_range dynamically, keeping first column and row aside.


回答1:


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);


来源:https://stackoverflow.com/questions/33428013/define-dynamic-cell-range

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!