--- 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,65 @@ } return clonedSheet; } + + /** + * Searches in the current Workbook for an equivalent CellStyle to the given one (from another Workbook). + * If none exists, it will be created. + * + * @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) { + + 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! + + for(int i = 0; i < this.getNumCellStyles(); i++){ + XSSFCellStyle currCellStyle = this.getCellStyleAt(i); + + 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()) + && Objects.equals(currCellStyle.getFont(), src.getFont()) + // TODO: Not a good method to compare... Any Ideas? + // Idea: Let the CellStyle decide itself it it equals another one (don't use .equals(), maybe .deepEquals()) + ){ + 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 +2531,6 @@ return oleId; } + + }