简体   繁体   English

使用Apache POI有条件地为行着色

[英]conditionally Coloring rows with Apache POI

I work by Apache poi API, I want to color the lines conditionally , the rule is alternating color after 5 lines: the line 1-5 red, line 6-10 blue, line 11-15 red. 我使用Apache poi API进行工作,我想有条件地为行着色,规则是在5行之后交替显示颜色:行1-5红色,行6-10蓝色,行11-15红色。 so on. 以此类推。

this rule : 这条规则:

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

alternate row in a different color. 不同颜色的替代行。

how I can write my rule ?? 我怎么写我的规则?

You'll need two rules, one for red and one for blue. 您将需要两个规则,一个规则用于红色,一个规则用于蓝色。 Subtract 1 from ROW() so that row 11 becomes 10 and row 15 becomes 14, so that the result of the mod is less than 5. Also, row 10 becomes 9 and the mod is greater than or equal to 5, so it will become blue. ROW()减去1,以使第11行变为10,而第15行变为14,因此mod的结果小于5。此外,第10行变为9且mod大于或等于5,因此它将变成蓝色。 The pattern repeats after 10 rows, so that's why the mod operand is 10 . 模式在10行之后重复,因此这就是mod操作数为10的原因。

ConditionalFormattingRule red = sheetCF.createConditionalFormattingRule(
    "MOD(ROW() - 1, 10) < 5");
FontFormatting ffRed = red.createFontFormatting();
ffRed.setFontColorIndex(IndexedColors.RED.index);

ConditionalFormattingRule blue = sheetCF.createConditionalFormattingRule(
    "MOD(ROW() - 1, 10) >= 5");
FontFormatting ffBlue = blue.createFontFormatting();
ffBlue.setFontColorIndex(IndexedColors.BLUE.index);

Then you can add the conditional formatting to your sheet with your SheetConditionalFormatting object, with an appropriate CellRangeAddress . 然后,您可以使用带有适当CellRangeAddress SheetConditionalFormatting对象将条件格式添加到工作表中。

For more detail, see the Quick Guide on Conditional Formatting . 有关更多详细信息,请参见《条件格式快速指南》

我认为以下方法(或类似方法,可能使用(ROW()+1)(ROW()-1)而不是ROW() )应该有效:

ConditionalFormattingRule rule1 = sheetCF.createConditionalFormattingRule("MOD(ROW() / 5,2)");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM