diff --git a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java index c7d1f85f32..b221bf5491 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFCellStyle.java @@ -829,6 +829,19 @@ public class XSSFCellStyle implements CellStyle, Duplicatable { addFill(ct); } + /** + * Set the background fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value. + *
+ * @param color the color to use + * @since POI 5.2.3 + */ + @Override + public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color) { + if (color instanceof XSSFColor) { + setFillBackgroundColor((XSSFColor)color); + } + } + /** * Set the background fill color represented as a indexed color value. *

@@ -882,6 +895,19 @@ public class XSSFCellStyle implements CellStyle, Duplicatable { addFill(ct); } + + /** + * Set the foreground fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value. + *
+ * @param color the color to use + * @since POI 5.2.3 + */ + @Override + public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color) { + if (color instanceof XSSFColor) { + setFillForegroundColor((XSSFColor)color); + } + } /** * Set the foreground fill color as a indexed color value diff --git a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java index d6408bc0a4..9bb826d56a 100644 --- a/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java +++ b/poi/src/main/java/org/apache/poi/hssf/usermodel/HSSFCellStyle.java @@ -637,6 +637,21 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable { _format.setFillBackground(bg); checkDefaultBackgroundFills(); } + + /** + * Set the background fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value. + *
+ * @param color the color to use + * @since POI 5.2.3 + */ + @Override + public void setFillBackgroundColor(org.apache.poi.ss.usermodel.Color color) + { + if (color instanceof HSSFColor) { + short index2 = ((HSSFColor)color).getIndex2(); + if (index2 != -1) setFillBackgroundColor(index2); + } + } /** * Get the background fill color. @@ -678,6 +693,21 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable { _format.setFillForeground(bg); checkDefaultBackgroundFills(); } + + /** + * Set the foreground fill color represented as a {@link org.apache.poi.ss.usermodel.Color} value. + *
+ * @param color the color to use + * @since POI 5.2.3 + */ + @Override + public void setFillForegroundColor(org.apache.poi.ss.usermodel.Color color) + { + if (color instanceof HSSFColor) { + short index2 = ((HSSFColor)color).getIndex2(); + if (index2 != -1) setFillForegroundColor(index2); + } + } /** * Get the foreground fill color. diff --git a/poi/src/main/java/org/apache/poi/ss/usermodel/CellStyle.java b/poi/src/main/java/org/apache/poi/ss/usermodel/CellStyle.java index 983ba99b9c..c4e539da8b 100644 --- a/poi/src/main/java/org/apache/poi/ss/usermodel/CellStyle.java +++ b/poi/src/main/java/org/apache/poi/ss/usermodel/CellStyle.java @@ -311,6 +311,15 @@ public interface CellStyle { */ void setFillBackgroundColor(short bg); + /** + * set the background fill color. + * use not a indexed color but a {@link org.apache.poi.ss.usermodel.Color) + * + * @param color org.apache.poi.ss.usermodel.Color to set + * @since POI 5.2.3 + */ + void setFillBackgroundColor(Color color); + /** * get the background fill color, if the fill * is defined with an indexed color. @@ -334,6 +343,15 @@ public interface CellStyle { */ void setFillForegroundColor(short bg); + /** + * set the foreground fill color. + * use not a indexed color but a {@link org.apache.poi.ss.usermodel.Color) + * + * @param color org.apache.poi.ss.usermodel.Color to set + * @since POI 5.2.3 + */ + void setFillForegroundColor(Color color); + /** * get the foreground fill color, if the fill * is defined with an indexed color. diff --git a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java index 184eafc91f..0665cefa38 100644 --- a/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java +++ b/poi/src/main/java/org/apache/poi/ss/util/CellUtil.java @@ -56,6 +56,10 @@ public final class CellUtil { public static final String DATA_FORMAT = "dataFormat"; public static final String FILL_BACKGROUND_COLOR = "fillBackgroundColor"; public static final String FILL_FOREGROUND_COLOR = "fillForegroundColor"; + + public static final String FILL_BACKGROUND_COLOR_COLOR = "fillBackgroundColorColor"; + public static final String FILL_FOREGROUND_COLOR_COLOR = "fillForegroundColorColor"; + public static final String FILL_PATTERN = "fillPattern"; public static final String FONT = "font"; public static final String HIDDEN = "hidden"; @@ -79,6 +83,13 @@ public final class CellUtil { DATA_FORMAT, ROTATION ))); + + private static final Set colorValues = Collections.unmodifiableSet( + new HashSet<>(Arrays.asList( + FILL_FOREGROUND_COLOR_COLOR, + FILL_BACKGROUND_COLOR_COLOR + ))); + private static final Set intValues = Collections.unmodifiableSet( new HashSet<>(Collections.singletonList( FONT @@ -376,6 +387,7 @@ public final class CellUtil { public static void setCellStyleProperties(Cell cell, Map properties) { Workbook workbook = cell.getSheet().getWorkbook(); CellStyle originalStyle = cell.getCellStyle(); + CellStyle newStyle = null; Map values = getFormatProperties(originalStyle); putAll(properties, values); @@ -447,8 +459,12 @@ public final class CellUtil { put(properties, BOTTOM_BORDER_COLOR, style.getBottomBorderColor()); put(properties, DATA_FORMAT, style.getDataFormat()); put(properties, FILL_PATTERN, style.getFillPattern()); + put(properties, FILL_FOREGROUND_COLOR, style.getFillForegroundColor()); put(properties, FILL_BACKGROUND_COLOR, style.getFillBackgroundColor()); + put(properties, FILL_FOREGROUND_COLOR_COLOR, style.getFillForegroundColorColor()); + put(properties, FILL_BACKGROUND_COLOR_COLOR, style.getFillBackgroundColorColor()); + put(properties, FONT, style.getFontIndex()); put(properties, HIDDEN, style.getHidden()); put(properties, INDENTION, style.getIndention()); @@ -475,6 +491,8 @@ public final class CellUtil { for (final String key : src.keySet()) { if (shortValues.contains(key)) { dest.put(key, getShort(src, key)); + } else if (colorValues.contains(key)) { + dest.put(key, getColor(src, key)); } else if (intValues.contains(key)) { dest.put(key, getInt(src, key)); } else if (booleanValues.contains(key)) { @@ -511,8 +529,12 @@ public final class CellUtil { style.setBottomBorderColor(getShort(properties, BOTTOM_BORDER_COLOR)); style.setDataFormat(getShort(properties, DATA_FORMAT)); style.setFillPattern(getFillPattern(properties, FILL_PATTERN)); + style.setFillForegroundColor(getShort(properties, FILL_FOREGROUND_COLOR)); style.setFillBackgroundColor(getShort(properties, FILL_BACKGROUND_COLOR)); + style.setFillForegroundColor(getColor(properties, FILL_FOREGROUND_COLOR_COLOR)); + style.setFillBackgroundColor(getColor(properties, FILL_BACKGROUND_COLOR_COLOR)); + style.setFont(workbook.getFontAt(getInt(properties, FONT))); style.setHidden(getBoolean(properties, HIDDEN)); style.setIndention(getShort(properties, INDENTION)); @@ -541,6 +563,23 @@ public final class CellUtil { } return 0; } + + /** + * Utility method that returns the named Color value from the given map. + * + * @param properties map of named properties (String -> Object) + * @param name property name + * @return null if the property does not exist, or is not a {@link Color} + * otherwise the property value + */ + private static Color getColor(Map properties, String name) { + Object value = properties.get(name); + if (value instanceof Color) { + return (Color) value; + } + return null; + } + /** * Utility method that returns the named int value from the given map.