View | Details | Raw Unified | Return to bug 60902
Collapse All | Expand All

(-)a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java (+77 lines)
Lines 37-42 Link Here
37
import java.util.Locale;
37
import java.util.Locale;
38
import java.util.Map;
38
import java.util.Map;
39
import java.util.NoSuchElementException;
39
import java.util.NoSuchElementException;
40
import java.util.Objects;
40
import java.util.regex.Pattern;
41
import java.util.regex.Pattern;
41
42
42
import javax.xml.namespace.QName;
43
import javax.xml.namespace.QName;
Lines 90-95 Link Here
90
import org.apache.poi.xssf.model.SharedStringsTable;
91
import org.apache.poi.xssf.model.SharedStringsTable;
91
import org.apache.poi.xssf.model.StylesTable;
92
import org.apache.poi.xssf.model.StylesTable;
92
import org.apache.poi.xssf.model.ThemesTable;
93
import org.apache.poi.xssf.model.ThemesTable;
94
import org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder.BorderSide;
93
import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils;
95
import org.apache.poi.xssf.usermodel.helpers.XSSFFormulaUtils;
94
import org.apache.xmlbeans.XmlException;
96
import org.apache.xmlbeans.XmlException;
95
import org.apache.xmlbeans.XmlObject;
97
import org.apache.xmlbeans.XmlObject;
Lines 659-664 Link Here
659
        }
661
        }
660
        return clonedSheet;
662
        return clonedSheet;
661
    }
663
    }
664
    
665
    /**
666
     * Copies a CellStyle from another Workbook to the current one. If the new CellStyle already exists (same font, fill, ...)
667
     * then the already existing one will be returned, otherwise a new one is created.
668
     * 
669
     * See {@link #cloneCellStyle(XSSFCellStyle, boolean)} if you want to simply add the new Style without any searching.
670
     *
671
     * @param src The CellStyle that is going to be cloned
672
     * @return CellStyle The created or looked up CellStyle in the current Workbook
673
     */
674
    public XSSFCellStyle cloneCellStyle(XSSFCellStyle src) {
675
        return cloneCellStyle(src, true);
676
    }
677
    
678
    /**
679
     * Copies a CellStyle from another Workbook to the current one. If the new CellStyle already exists (same font, fill, ...)
680
     * - 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.
681
     *
682
     * @param src The CellStyle that is going to be cloned
683
     * @param clean If set to true, this method attempts to find a already existing CellStyle which has the same properties first
684
     * @return CellStyle The created or looked up CellStyle in the current Workbook
685
     */
686
    public XSSFCellStyle cloneCellStyle(XSSFCellStyle src, boolean clean) {
687
        
688
        XSSFCellStyle returnStyle = null;
689
        
690
        // Search for an existing Style that fits the source
691
        // Caution: Even if the Style is the same, the underlying borders, fills, ... may differ!
692
        if(clean){
693
            for(int i = 0; i < this.getNumCellStyles(); i++){
694
                XSSFCellStyle currCellStyle = this.getCellStyleAt(i);
695
                
696
                // TODO: Order from "most probably not the same" to "most probably the same" to increase performance
697
                //       e.g.: It is more likely that the Font changed than WrapText
698
                // TODO: Let the CellStyle decide if it equals another one (CellStyle.deepEquals()?!)
699
                if(Objects.equals(currCellStyle.getHidden(), src.getHidden())
700
                        && Objects.equals(currCellStyle.getAlignmentEnum(), src.getAlignmentEnum())
701
                        && Objects.equals(currCellStyle.getBorderBottomEnum(), src.getBorderBottomEnum())
702
                        && Objects.equals(currCellStyle.getRotation(), src.getRotation())
703
                        && Objects.equals(currCellStyle.getFillPatternEnum(), src.getFillPatternEnum())
704
                        && Objects.equals(currCellStyle.getFillBackgroundXSSFColor(), src.getFillBackgroundXSSFColor())
705
                        && Objects.equals(currCellStyle.getFillForegroundXSSFColor(), src.getFillForegroundXSSFColor())
706
                        && Objects.equals(currCellStyle.getFont(), src.getFont())
707
                        && Objects.equals(currCellStyle.getBorderTopEnum(), src.getBorderTopEnum())
708
                        && Objects.equals(currCellStyle.getTopBorderXSSFColor(), src.getTopBorderXSSFColor())
709
                        && Objects.equals(currCellStyle.getBorderRightEnum(), src.getBorderRightEnum())
710
                        && Objects.equals(currCellStyle.getRightBorderXSSFColor(), src.getRightBorderXSSFColor())
711
                        && Objects.equals(currCellStyle.getBorderBottomEnum(), src.getBorderBottomEnum())
712
                        && Objects.equals(currCellStyle.getBottomBorderXSSFColor(), src.getBottomBorderXSSFColor())
713
                        && Objects.equals(currCellStyle.getBorderLeftEnum(), src.getBorderLeftEnum())
714
                        && Objects.equals(currCellStyle.getLeftBorderXSSFColor(), src.getLeftBorderXSSFColor())
715
                        && Objects.equals(currCellStyle.getDataFormat(), src.getDataFormat())
716
                        && Objects.equals(currCellStyle.getIndention(), src.getIndention())
717
                        && Objects.equals(currCellStyle.getLocked(), src.getLocked())
718
                        && Objects.equals(currCellStyle.getQuotePrefixed(), src.getQuotePrefixed())
719
                        && Objects.equals(currCellStyle.getShrinkToFit(), src.getShrinkToFit())
720
                        && Objects.equals(currCellStyle.getVerticalAlignmentEnum(), src.getVerticalAlignmentEnum())
721
                        && Objects.equals(currCellStyle.getWrapText(), src.getWrapText())
722
                        ){
723
                    returnStyle = currCellStyle;
724
                }
725
            }
726
        }
727
            
728
        if(returnStyle == null){
729
            // If none exists: Create one (use XSSFCellStyles cloneStyle)
730
            returnStyle = this.createCellStyle();
731
            returnStyle.cloneStyleFrom(src);
732
        }
733
734
        return returnStyle;
735
    }
736
    
662
    
737
    
663
    /**
738
    /**
664
     * @since 3.14-Beta1
739
     * @since 3.14-Beta1
Lines 2470-2473 Link Here
2470
2545
2471
        return oleId;
2546
        return oleId;
2472
    }
2547
    }
2548
2549
2473
}
2550
}

Return to bug 60902