Apache POI - 基于公式的条件格式 (XSSFFontFormatting) 的主题颜色
Apache POI - Theme color for formula based Conditional formatting (XSSFFontFormatting)
我正在尝试使用 poi api 根据公式创建条件格式。通过读取另一个 (template/reference) 单元格来设置字体颜色。问题是当前 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
感谢您调查我的问题!!
XSSFFontFormatting.setFontColor(颜色)中存在错误。它只设置 RGB 值而不设置 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;
}
我正在尝试使用 poi api 根据公式创建条件格式。通过读取另一个 (template/reference) 单元格来设置字体颜色。问题是当前 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
感谢您调查我的问题!!
XSSFFontFormatting.setFontColor(颜色)中存在错误。它只设置 RGB 值而不设置 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;
}