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 a9d1c76ad3..3c4252b65e 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 @@ -29,6 +29,7 @@ import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.IndexedColors; import org.apache.poi.ss.usermodel.ReadingOrder; import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.util.CellUtil; import org.apache.poi.util.Internal; import org.apache.poi.util.Removal; import org.apache.poi.xssf.model.StylesTable; @@ -202,7 +203,7 @@ public class XSSFCellStyle implements CellStyle, Duplicatable { _font = null; _cellAlignment = null; } else { - throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle"); + CellUtil.cloneStyle(source, this, null); } } 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 0983c45497..d1fe755fc3 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 @@ -35,6 +35,7 @@ import org.apache.poi.ss.usermodel.FillPatternType; import org.apache.poi.ss.usermodel.Font; import org.apache.poi.ss.usermodel.HorizontalAlignment; import org.apache.poi.ss.usermodel.VerticalAlignment; +import org.apache.poi.ss.util.CellUtil; import org.apache.poi.util.Removal; import org.apache.poi.util.ThreadLocalUtil; @@ -49,22 +50,29 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable { private final ExtendedFormatRecord _format; private final short _index; private final InternalWorkbook _workbook; + private final HSSFWorkbook _hssfWorkbook; - - /** Creates new HSSFCellStyle why would you want to do this?? */ protected HSSFCellStyle(short index, ExtendedFormatRecord rec, HSSFWorkbook workbook) { - this(index, rec, workbook.getWorkbook()); + _workbook = workbook.getInternalWorkbook(); + _hssfWorkbook = workbook; + _index = index; + _format = rec; } + + @Deprecated + @Removal(version = "7.0.0") protected HSSFCellStyle(short index, ExtendedFormatRecord rec, InternalWorkbook workbook) { _workbook = workbook; + _hssfWorkbook = null; _index = index; - _format = rec; + _format = rec; } protected HSSFCellStyle(HSSFCellStyle other) { _workbook = other._workbook; + _hssfWorkbook = other._hssfWorkbook; _index = other._index; _format = other._format; } @@ -850,8 +858,8 @@ public final class HSSFCellStyle implements CellStyle, Duplicatable { public void cloneStyleFrom(CellStyle source) { if(source instanceof HSSFCellStyle) { this.cloneStyleFrom((HSSFCellStyle)source); - } else { - throw new IllegalArgumentException("Can only clone from one HSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle"); + } else if (_hssfWorkbook != null) { + CellUtil.cloneStyle(source, this, _hssfWorkbook); } } public void cloneStyleFrom(HSSFCellStyle source) { 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 e157bfa1fb..d0ee0db55b 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 @@ -47,6 +47,7 @@ import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.VerticalAlignment; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.util.Beta; +import org.apache.poi.util.Internal; import org.apache.poi.util.Removal; /** @@ -806,7 +807,7 @@ public final class CellUtil { * Sets the format properties of the given style based on the given map. * * @param style cell style - * @param workbook parent workbook + * @param workbook parent workbook (can be null but some fomt info will not be copied if null is passed) * @param properties map of format properties (CellPropertyType -> Object) * @see #getFormatProperties(CellStyle) */ @@ -848,7 +849,9 @@ public final class CellUtil { } } - style.setFont(workbook.getFontAt(getInt(properties, CellPropertyType.FONT))); + if (workbook != null) { + style.setFont(workbook.getFontAt(getInt(properties, CellPropertyType.FONT))); + } style.setHidden(getBoolean(properties, CellPropertyType.HIDDEN)); style.setIndention(getShort(properties, CellPropertyType.INDENTION)); style.setLeftBorderColor(getShort(properties, CellPropertyType.LEFT_BORDER_COLOR)); @@ -861,6 +864,25 @@ public final class CellUtil { style.setQuotePrefixed(getBoolean(properties, CellPropertyType.QUOTE_PREFIXED)); } + /** + * Clones the style from one cell to another. For internal use only. + * Users should use the cloneStyleFrom method on CellStyle instead. + * + * @param src source cell style + * @param dest destination cell style + * @param destWorkbook destination workbook (can be null but some font info will not be copied if null is passed) + * @throws IllegalArgumentException if source or destination styles are null + * @since POI 5.4.2 + */ + @Internal + public static void cloneStyle(CellStyle src, CellStyle dest, Workbook destWorkbook) { + if (src == null || dest == null) { + throw new IllegalArgumentException("Source and destination styles must not be null"); + } + EnumMap properties = getFormatProperties(src); + setFormatProperties(dest, destWorkbook, properties); + } + /** * Utility method that returns the named short value from the given map. *