【问题标题】:Apache POI - Theme color for formula based Conditional formatting (XSSFFontFormatting)Apache POI - 基于公式的条件格式的主题颜色 (XSSFFontFormatting)
【发布时间】:2020-02-27 17:56:20
【问题描述】:

我正在尝试使用 poi api 基于公式创建条件格式。通过读取另一个(模板/参考)单元格来设置字体颜色。问题是当前的 api 不支持基于公式的条件的基于主题的颜色。我错过了什么吗?有什么办法可以做到吗?

    public void test(XSSFCellStyle style, String complexFormula){
        ....
        XSSFConditionalFormattingRule rule = (XSSFConditionalFormattingRule )
                      sheetCF.createConditionalFormattingRule(complexFormula);
        XSSFConditionalFormattingRule (rule,style);
    }

    protected void createConditionalFormatingRules(XSSFConditionalFormattingRule rule, XSSFCellStyle style) {

        XSSFFontFormatting fontFmt = (XSSFFontFormatting) rule.createFontFormatting();
        XSSFFont font = style.getFont();

        fontFmt.setFontColorIndex(font.getXSSFColor());  // BROKEN -- this doe not work for theme color

        fontFmt.setFontHeight(font.getFontHeight());
        fontFmt.setUnderlineType(font.getUnderline());
        fontFmt.setFontStyle(font.getItalic(), font.getBold());
        fontFmt.setEscapementType(FontFormatting.SS_NONE);
    }

以上,fontFmt.setFontColorIndex(font.getXSSFColor()); 不适用于主题颜色。它适用于标准颜色。

poi-ooxml-4.1.0

感谢您调查我的问题!

【问题讨论】:

    标签: java apache-poi openxml xssf


    【解决方案1】:

    XSSFFontFormatting.setFontColor(Color) 中有一个错误。它只设置 RGB 值,但不设置 Tint 值。

    我通过添加以下代码来设置 Tint 解决了这个问题。

            XSSFColor color = font.getXSSFColor();
            if(color != null) {
                fontFmt.setFontColor(color);
                //Themed color is not handed properly by poi. Hence have to handle it below.
                if(color.isThemed()) {
                    CTFont ctFont = (CTFont) getFieldValWithReflection(fontFmt,"_font");
                    if(ctFont != null && ctFont.sizeOfColorArray()>0){
                        CTColor c = ctFont.getColorArray(0);
                        if(color.hasTint())
                            c.setTint(font.getXSSFColor().getTint());
                        if(color.isIndexed())
                            c.setIndexed(color.getIndexed());
                    }
                }
            }
    
        /**
         * Helper for the very common case of having to get underlying XML data.
         */
        private Object getFieldValWithReflection(Object owner, String fieldName) {
            Field f = null;
            Object val = null;
            try {
                f = owner.getClass().getDeclaredField(fieldName);
                f.setAccessible(true);
    
                val = f.get(owner);
                return val;
    
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (f != null) {
                    f.setAccessible(false);
                }
            }
    
            return null;
        }
    
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-08-18
      • 2013-01-13
      • 2017-01-20
      相关资源
      最近更新 更多