try best effort clone of styles if types don't match

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1925524 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
PJ Fanning 2025-05-12 22:27:42 +00:00
parent dce1a83169
commit 2f55495ba9
3 changed files with 40 additions and 9 deletions

View File

@ -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);
}
}

View File

@ -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) {

View File

@ -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<CellPropertyType, Object> properties = getFormatProperties(src);
setFormatProperties(dest, destWorkbook, properties);
}
/**
* Utility method that returns the named short value from the given map.
*