java - 如何使用java定义Excel工作表的动态单元格范围?

标签 java excel apache-poi

这是我的代码:

SheetConditionalFormatting sheetCF = sheet.getSheetConditionalFormatting();
ConditionalFormattingRule rule = sheetCF.createConditionalFormattingRule( "MOD(ROW() - 1, 1) = 0");
PatternFormatting fill = rule.createPatternFormatting();
style.setFillForegroundColor(IndexedColors.BLUE.index);
style.setFillPattern(CellStyle.SOLID_FOREGROUND);

FontFormatting ffRed = rule.createFontFormatting();
ffRed.setFontColorIndex(IndexedColors.WHITE.index);

CellRangeAddress[] regions = {  CellRangeAddress.valueOf("A1:ZZ1") };

sheetCF.addConditionalFormatting(regions,rule);

ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule(
                        "MOD(ROW() - 1, 2) = 0");

PatternFormatting fill1 = rule1.createPatternFormatting();
fill1.setFillBackgroundColor(IndexedColors.PALE_BLUE.index);
fill1.setFillPattern(PatternFormatting.SOLID_FOREGROUND);

CellRangeAddress[] regions1 = {  CellRangeAddress.valueOf("A2:ZZ8304") };

sheetCF.addConditionalFormatting(regions1,rule1);

在 CellRangeAddress.values 中我给出了静态值。但我希望它是动态的。也就是说,只要有数据存在,它就应该动态变化。如何解决这个问题?我提到了define dynamic cell range但没有成功。

最佳答案

你可以这样尝试

String name = CellReference.convertNumToColString(cell.getColumnIndex());
    String location = "$" + name + "$" + cell.getRowIndex() + ":$" + name + "$" + (cell.getRowIndex() + 1);

    CellRangeAddress[] regions = { CellRangeAddress.valueOf(location) };

关于java - 如何使用java定义Excel工作表的动态单元格范围?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59480696/

相关文章:

java - LibGDX 中纹理的多个实例?

java - 启用断言时的系统类,它们是什么?

c# - VSTO 加载项 : Reference in the manifest does not match the identity of the downloaded assembly

java - Apache poi 单元格格式

java - CellStyle共享同一个堆地址apache poi

java - 创建 Java 库

vba - 卡在删除存储在变体数据类型中的记录

vba - 在数据验证下拉列表中设置默认值

java - 新版本 POI 中缺少 poi-contrib-*.jar

java - 嵌套 for 循环和变量的放置