--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java +++ a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java @@ -37,6 +37,7 @@ import java.util.Locale; import java.util.Map; import java.util.NoSuchElementException; +import java.util.Objects; import java.util.regex.Pattern; import javax.xml.namespace.QName; @@ -90,6 +91,7 @@ import org.apache.poi.xssf.model.SharedStringsTable; import org.apache.poi.xssf.model.StylesTable; import org.apache.poi.xssf.model.ThemesTable; +import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide; import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils; import org.apache.xmlbeans.XmlException; import org.apache.xmlbeans.XmlObject; @@ -659,6 +661,79 @@ } return clonedSheet; } + + /** + * Copies a CellStyle from another Workbook to the current one. If the new CellStyle already exists (same font, fill, ...) + * then the already existing one will be returned, otherwise a new one is created. + * + * See {@link #cloneCellStyle(XSSFCellStyle, boolean)} if you want to simply add the new Style without any searching. + * + * @param src The CellStyle that is going to be cloned + * @return CellStyle The created or looked up CellStyle in the current Workbook + */ + public XSSFCellStyle cloneCellStyle(XSSFCellStyle src) { + return cloneCellStyle(src, true); + } + + /** + * Copies a CellStyle from another Workbook to the current one. If the new CellStyle already exists (same font, fill, ...) + * - and clean is set to true - then the already existing one will be returned, otherwise a new one is created, added to the workbook and then returned. + * + * @param src The CellStyle that is going to be cloned + * @param clean If set to true, this method attempts to find a already existing CellStyle which has the same properties first + * @return CellStyle The created or looked up CellStyle in the current Workbook + */ + public XSSFCellStyle cloneCellStyle(XSSFCellStyle src, boolean clean) { + + XSSFCellStyle returnStyle = null; + + // Search for an existing Style that fits the source + // Caution: Even if the Style is the same, the underlying borders, fills, ... may differ! + if(clean){ + for(int i = 0; i < this.getNumCellStyles(); i++){ + XSSFCellStyle currCellStyle = this.getCellStyleAt(i); + + // TODO: Order from "most probably not the same" to "most probably the same" to increase performance + // e.g.: It is more likely that the Font changed than WrapText + // TODO: Let the CellStyle decide if it equals another one (CellStyle.deepEquals()?!) + if(Objects.equals(currCellStyle.getHidden(), src.getHidden()) + && Objects.equals(currCellStyle.getAlignmentEnum(), src.getAlignmentEnum()) + && Objects.equals(currCellStyle.getBorderBottomEnum(), src.getBorderBottomEnum()) + && Objects.equals(currCellStyle.getRotation(), src.getRotation()) + && Objects.equals(currCellStyle.getFillPatternEnum(), src.getFillPatternEnum()) + && Objects.equals(currCellStyle.getFillBackgroundXSSFColor(), src.getFillBackgroundXSSFColor()) + && Objects.equals(currCellStyle.getFillForegroundXSSFColor(), src.getFillForegroundXSSFColor()) + && Objects.equals(currCellStyle.getFont(), src.getFont()) + && Objects.equals(currCellStyle.getBorderTopEnum(), src.getBorderTopEnum()) + && Objects.equals(currCellStyle.getTopBorderXSSFColor(), src.getTopBorderXSSFColor()) + && Objects.equals(currCellStyle.getBorderRightEnum(), src.getBorderRightEnum()) + && Objects.equals(currCellStyle.getRightBorderXSSFColor(), src.getRightBorderXSSFColor()) + && Objects.equals(currCellStyle.getBorderBottomEnum(), src.getBorderBottomEnum()) + && Objects.equals(currCellStyle.getBottomBorderXSSFColor(), src.getBottomBorderXSSFColor()) + && Objects.equals(currCellStyle.getBorderLeftEnum(), src.getBorderLeftEnum()) + && Objects.equals(currCellStyle.getLeftBorderXSSFColor(), src.getLeftBorderXSSFColor()) + && Objects.equals(currCellStyle.getDataFormat(), src.getDataFormat()) + && Objects.equals(currCellStyle.getIndention(), src.getIndention()) + && Objects.equals(currCellStyle.getLocked(), src.getLocked()) + && Objects.equals(currCellStyle.getQuotePrefixed(), src.getQuotePrefixed()) + && Objects.equals(currCellStyle.getShrinkToFit(), src.getShrinkToFit()) + && Objects.equals(currCellStyle.getVerticalAlignmentEnum(), src.getVerticalAlignmentEnum()) + && Objects.equals(currCellStyle.getWrapText(), src.getWrapText()) + ){ + returnStyle = currCellStyle; + } + } + } + + if(returnStyle == null){ + // If none exists: Create one (use XSSFCellStyles cloneStyle) + returnStyle = this.createCellStyle(); + returnStyle.cloneStyleFrom(src); + } + + return returnStyle; + } + /** * @since 3.14-Beta1 @@ -2470,4 +2545,6 @@ return oleId; } + + }