具有公式的Apache POI颜色单元格

时间:2009-05-06 09:02:12

标签: java excel apache-poi

我想在java中使用Apache POI创建一个excel,我必须在单元格中插入一个公式:A3 = B3 + C3。

可以在A3中插入另一个公式,如果其值为>则为该单元着色。 0?

我使用Apache POI 2.5.1

1 个答案:

答案 0 :(得分:9)

您需要conditional formatting

从这份文件:

 // 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);

创建一个红色背景的单元格,值为> = 100.这几乎是你想要的: - )