gpt4 book ai didi

java - Apache POI 带有公式的颜色单元格

转载 作者:行者123 更新时间:2023-12-02 03:11:20 25 4
gpt4 key购买 nike

我想使用 Java 中的 Apache POI 创建 Excel,并且必须在单元格中插入公式:A3=B3+C3。

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

我使用 Apache POI 2.5.1

最佳答案

您将需要 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。这几乎就是您想要的:-)

关于java - Apache POI 带有公式的颜色单元格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/828721/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com