diff options
author | PJ Fanning <fanningpj@apache.org> | 2022-07-10 10:12:20 +0000 |
---|---|---|
committer | PJ Fanning <fanningpj@apache.org> | 2022-07-10 10:12:20 +0000 |
commit | 6602122926db0bc9623811675b8fed2902b41ce4 (patch) | |
tree | 6175771a450778b681380f999e3ebffefb78ca48 /poi | |
parent | d9383d6ba9b2f90b7d3fb7fc45ae22c2e7547368 (diff) | |
download | poi-6602122926db0bc9623811675b8fed2902b41ce4.tar.gz poi-6602122926db0bc9623811675b8fed2902b41ce4.zip |
[bug-66052] apply cell style fixes supplied by Axel Richter
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1902622 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'poi')
3 files changed, 87 insertions, 0 deletions
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. + * <br> + * @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. + * <br> + * @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 @@ -312,6 +312,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. * @return fill color index, or 0 if not indexed (XSSF only) @@ -335,6 +344,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. * @return fill color, or 0 if not indexed (XSSF only) 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<String> colorValues = Collections.unmodifiableSet( + new HashSet<>(Arrays.asList( + FILL_FOREGROUND_COLOR_COLOR, + FILL_BACKGROUND_COLOR_COLOR + ))); + private static final Set<String> intValues = Collections.unmodifiableSet( new HashSet<>(Collections.singletonList( FONT @@ -376,6 +387,7 @@ public final class CellUtil { public static void setCellStyleProperties(Cell cell, Map<String, Object> properties) { Workbook workbook = cell.getSheet().getWorkbook(); CellStyle originalStyle = cell.getCellStyle(); + CellStyle newStyle = null; Map<String, Object> 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<String, Object> 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. |