Apache POI Cellule de couleur avec formule
-
06-07-2019 - |
Question
Je veux créer un excel avec Apache POI en Java et je dois insérer dans une cellule une formule: A3 = B3 + C3.
Il est possible d’insérer une autre formule en A3 qui colorie la cellule si sa valeur est > 0?
J'utilise Apache POI 2.5.1
La solution
Vous aurez besoin d'un formatage conditionnel . .
À partir de ce document:
// Define a Conditional Formatting rule, which triggers formatting
// when cell's value is greater or equal than 100.0 and
// applies patternFormatting defined below.
HSSFConditionalFormattingRule rule = sheet.createConditionalFormattingRule(
ComparisonOperator.GE,
"100.0", // 1st formula
null // 2nd formula is not used for comparison operator GE
);
// Create pattern with red background
HSSFPatternFormatting patternFmt = rule.cretePatternFormatting();
patternFormatting.setFillBackgroundColor(HSSFColor.RED.index);
// Define a region containing first column
Region [] regions =
{
new Region(1,(short)1,-1,(short)1)
};
// Apply Conditional Formatting rule defined above to the regions
sheet.addConditionalFormatting(regions, rule);
qui crée une cellule avec un arrière-plan rouge pour les valeurs > = 100. C’est ce que vous voulez presque: -)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow