Search in sources :

Example 6 with CTFill

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill in project poi by apache.

the class TestXSSFCellStyle method testGetSetFillBackgroundColor.

@Test
public void testGetSetFillBackgroundColor() {
    assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
    assertNull(cellStyle.getFillBackgroundXSSFColor());
    XSSFColor clr;
    int num = stylesTable.getFills().size();
    //setting indexed color
    cellStyle.setFillBackgroundColor(IndexedColors.RED.getIndex());
    assertEquals(IndexedColors.RED.getIndex(), cellStyle.getFillBackgroundColor());
    clr = cellStyle.getFillBackgroundXSSFColor();
    assertTrue(clr.getCTColor().isSetIndexed());
    assertEquals(IndexedColors.RED.getIndex(), clr.getIndexed());
    //a new fill was added to the styles table
    assertEquals(num + 1, stylesTable.getFills().size());
    //id of the created border
    int fillId = (int) cellStyle.getCoreXf().getFillId();
    assertTrue(fillId > 0);
    //check changes in the underlying xml bean
    CTFill ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
    assertEquals(IndexedColors.RED.getIndex(), ctFill2.getPatternFill().getBgColor().getIndexed());
    //setting XSSFColor
    num = stylesTable.getFills().size();
    clr = new XSSFColor(java.awt.Color.CYAN);
    cellStyle.setFillBackgroundColor(clr);
    assertEquals(clr.getCTColor().toString(), cellStyle.getFillBackgroundXSSFColor().getCTColor().toString());
    byte[] rgb = cellStyle.getFillBackgroundXSSFColor().getRGB();
    assertEquals(java.awt.Color.CYAN, new java.awt.Color(rgb[0] & 0xFF, rgb[1] & 0xFF, rgb[2] & 0xFF));
    //another border was added to the styles table
    assertEquals(num + 1, stylesTable.getFills().size());
    //passing null unsets the color
    cellStyle.setFillBackgroundColor(null);
    assertNull(cellStyle.getFillBackgroundXSSFColor());
    assertEquals(IndexedColors.AUTOMATIC.getIndex(), cellStyle.getFillBackgroundColor());
}
Also used : CTFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill) Test(org.junit.Test)

Example 7 with CTFill

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill in project poi by apache.

the class TestXSSFCellStyle method testGetFillPattern.

@Test
public void testGetFillPattern() {
    assertEquals(STPatternType.INT_DARK_GRAY - 1, cellStyle.getFillPatternEnum().getCode());
    assertEquals(STPatternType.INT_DARK_GRAY - 1, cellStyle.getFillPattern());
    int num = stylesTable.getFills().size();
    cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    assertEquals(FillPatternType.SOLID_FOREGROUND, cellStyle.getFillPatternEnum());
    assertEquals(CellStyle.SOLID_FOREGROUND, cellStyle.getFillPattern());
    assertEquals(num + 1, stylesTable.getFills().size());
    int fillId = (int) cellStyle.getCoreXf().getFillId();
    assertTrue(fillId > 0);
    //check changes in the underlying xml bean
    CTFill ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
    assertEquals(STPatternType.SOLID, ctFill2.getPatternFill().getPatternType());
    //setting the same fill multiple time does not update the styles table
    for (int i = 0; i < 3; i++) {
        cellStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
    }
    assertEquals(num + 1, stylesTable.getFills().size());
    cellStyle.setFillPattern(FillPatternType.NO_FILL);
    assertEquals(FillPatternType.NO_FILL, cellStyle.getFillPatternEnum());
    assertEquals(CellStyle.NO_FILL, cellStyle.getFillPattern());
    fillId = (int) cellStyle.getCoreXf().getFillId();
    ctFill2 = stylesTable.getFillAt(fillId).getCTFill();
    assertFalse(ctFill2.getPatternFill().isSetPatternType());
}
Also used : CTFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill) Test(org.junit.Test)

Example 8 with CTFill

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill in project poi by apache.

the class XSSFCellStyle method cloneStyleFrom.

/**
     * Clones all the style information from another
     *  XSSFCellStyle, onto this one. This
     *  XSSFCellStyle will then have all the same
     *  properties as the source, but the two may
     *  be edited independently.
     * Any stylings on this XSSFCellStyle will be lost!
     *
     * The source XSSFCellStyle could be from another
     *  XSSFWorkbook if you like. This allows you to
     *  copy styles from one XSSFWorkbook to another.
     */
@Override
public void cloneStyleFrom(CellStyle source) {
    if (source instanceof XSSFCellStyle) {
        XSSFCellStyle src = (XSSFCellStyle) source;
        // Is it on our Workbook?
        if (src._stylesSource == _stylesSource) {
            // Nice and easy
            _cellXf.set(src.getCoreXf());
            _cellStyleXf.set(src.getStyleXf());
        } else {
            // Copy the style
            try {
                //  avoid orphaned nodes
                if (_cellXf.isSetAlignment())
                    _cellXf.unsetAlignment();
                if (_cellXf.isSetExtLst())
                    _cellXf.unsetExtLst();
                // Create a new Xf with the same contents
                _cellXf = CTXf.Factory.parse(src.getCoreXf().toString(), DEFAULT_XML_OPTIONS);
                // bug 56295: ensure that the fills is available and set correctly
                CTFill fill = CTFill.Factory.parse(src.getCTFill().toString(), DEFAULT_XML_OPTIONS);
                addFill(fill);
                // bug 58084: set borders correctly
                CTBorder border = CTBorder.Factory.parse(src.getCTBorder().toString(), DEFAULT_XML_OPTIONS);
                addBorder(border);
                // Swap it over
                _stylesSource.replaceCellXfAt(_cellXfId, _cellXf);
            } catch (XmlException e) {
                throw new POIXMLException(e);
            }
            // Copy the format
            String fmt = src.getDataFormatString();
            setDataFormat((new XSSFDataFormat(_stylesSource)).getFormat(fmt));
            // Copy the font
            try {
                CTFont ctFont = CTFont.Factory.parse(src.getFont().getCTFont().toString(), DEFAULT_XML_OPTIONS);
                XSSFFont font = new XSSFFont(ctFont);
                font.registerTo(_stylesSource);
                setFont(font);
            } catch (XmlException e) {
                throw new POIXMLException(e);
            }
        }
        // Clear out cached details
        _font = null;
        _cellAlignment = null;
    } else {
        throw new IllegalArgumentException("Can only clone from one XSSFCellStyle to another, not between HSSFCellStyle and XSSFCellStyle");
    }
}
Also used : CTFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill) CTFont(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont) CTBorder(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder) XmlException(org.apache.xmlbeans.XmlException) POIXMLException(org.apache.poi.POIXMLException)

Example 9 with CTFill

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill in project poi by apache.

the class XSSFCellStyle method setFillBackgroundColor.

/**
     * Set the background fill color represented as a {@link XSSFColor} value.
     * <p>
     * For example:
     * <pre>
     * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
     * cs.setFillBackgroundXSSFColor(new XSSFColor(java.awt.Color.RED));
     * </pre>
     * optionally a Foreground and background fill can be applied:
     * <i>Note: Ensure Foreground color is set prior to background</i>
     * <pre>
     * cs.setFillPattern(XSSFCellStyle.FINE_DOTS );
     * cs.setFillForegroundColor(new XSSFColor(java.awt.Color.BLUE));
     * cs.setFillBackgroundColor(new XSSFColor(java.awt.Color.GREEN));
     * </pre>
     * or, for the special case of SOLID_FILL:
     * <pre>
     * cs.setFillPattern(XSSFCellStyle.SOLID_FOREGROUND );
     * cs.setFillForegroundColor(new XSSFColor(java.awt.Color.GREEN));
     * </pre>
     * It is necessary to set the fill style in order
     * for the color to be shown in the cell.
     *
     * @param color - the color to use
     */
public void setFillBackgroundColor(XSSFColor color) {
    CTFill ct = getCTFill();
    CTPatternFill ptrn = ct.getPatternFill();
    if (color == null) {
        if (ptrn != null && ptrn.isSetBgColor())
            ptrn.unsetBgColor();
    } else {
        if (ptrn == null)
            ptrn = ct.addNewPatternFill();
        ptrn.setBgColor(color.getCTColor());
    }
    addFill(ct);
}
Also used : CTFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill) CTPatternFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill)

Example 10 with CTFill

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill in project poi by apache.

the class TestXSSFCellFill method testGetFillBackgroundColor.

@Test
public void testGetFillBackgroundColor() {
    CTFill ctFill = CTFill.Factory.newInstance();
    XSSFCellFill cellFill = new XSSFCellFill(ctFill, null);
    CTPatternFill ctPatternFill = ctFill.addNewPatternFill();
    CTColor bgColor = ctPatternFill.addNewBgColor();
    assertNotNull(cellFill.getFillBackgroundColor());
    bgColor.setIndexed(2);
    assertEquals(2, cellFill.getFillBackgroundColor().getIndexed());
}
Also used : CTFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill) CTPatternFill(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill) CTColor(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor) Test(org.junit.Test)

Aggregations

CTFill (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill)11 CTPatternFill (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPatternFill)7 Test (org.junit.Test)6 CTColor (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTColor)2 POIXMLException (org.apache.poi.POIXMLException)1 XSSFCellFill (org.apache.poi.xssf.usermodel.extensions.XSSFCellFill)1 XmlException (org.apache.xmlbeans.XmlException)1 CTBorder (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder)1 CTFont (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont)1