阿帕奇POI彩色细胞与公式

时间:2021-11-30 15:39:34

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

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

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

如果单元格的值是>,那么是否可以在A3中插入另一个公式来给单元格着色?

I use Apache POI 2.5.1

我使用Apache POI 2.5.1

1 个解决方案

#1


8  

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

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

#1


8  

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

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