簡體   English   中英

如何使用java為Excel工作表定義動態單元格范圍?

[英]How to define dynamic cell range for Excel sheet using java?

這是我的代碼:

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 中,我給出了靜態值。 但我希望它是動態的。 也就是說,只要有數據存在,它就應該動態地獲取范圍。 如何解決這個問題? 我提到定義動態單元格范圍,但它沒有用。

你可以這樣試試

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM