2017-11-10 147 views
1

尝试使用POI条件格式。我有以下方法:使用POI FontFormatting时出现异常setFontColor

private final static Color PEAK_ORANGE = new Color(255, 239, 221); 

public ConditionalFormattingRule getConditionalFormatting(ConditionalFormattingRule formattingContainer, FormatSpecs format){ 

    FontFormatting fontFmt = formattingContainer.createFontFormatting(); 
    fontFmt.setFontStyle(true, false); 
    // fontFmt.setFontColorIndex((short)11); 
    fontFmt.setFontColor(new XSSFColor(PEAK_ORANGE)); 

    PatternFormatting patternFmt = formattingContainer.createPatternFormatting(); 
    patternFmt.setFillBackgroundColor(new XSSFColor(PEAK_ORANGE)); 
    return formattingContainer; 
} 

当我使用setFontColor()方法,我得到索引越界异常。

enter image description here

当我使用setFontColorIndex()方法使用某种任意索引值,我没有得到例外。不过请注意,我在调用中使用完全相同的颜色参考来设置背景颜色

patternFmt.setFillBackgroundColor(new XSSFColor(PEAK_ORANGE));

这个工作正常,没有例外。

有没有其他人遇到过这个问题?我在设置字体颜色的过程中是否缺少某些东西?我更喜欢使用我的颜色,而不是来自IndexedColors类的颜色。

回答

2

这看起来像在Apache的POI的小bug,一个简单的解决方法是,首先设置一个索引色,然后设置实际预期全色,即

fontFmt.setFontColorIndex((short)1); 
    fontFmt.setFontColor(new XSSFColor(PEAK_ORANGE)); 

setFontColorIndex()将初始化内部结构,以便即setFontColor()将工作。

仅供参考,应该及时修复Apache POI 4.0版的bug。