views:

1073

answers:

1

I want create a excel with Apache POI in java and I must insert in a cell a formula: A3=B3+C3.

Is possible to insert another formula in A3 that color the cell if his value is> 0?

I use Apache POI 2.5.1

+3  A: 

You will need a conditional formatting.

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

which creates a cell with a red background for values >= 100. Which is almost what you want :-)

Brian Agnew