当前位置: 动力学知识库 > 问答 > 编程问答 >

java - Apache POI Color cell with formula

问题描述:

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

网友答案:

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 :-)

分享给朋友:
您可能感兴趣的文章:
随机阅读: