Search in sources :

Example 16 with CTBorder

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

the class TestXSSFCellStyle method testGetSetBorderTop.

@Test
public void testGetSetBorderTop() {
    //default values
    assertEquals(BorderStyle.NONE, cellStyle.getBorderTopEnum());
    int num = stylesTable.getBorders().size();
    cellStyle.setBorderTop(BorderStyle.MEDIUM);
    assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderTopEnum());
    //a new border has been added
    assertEquals(num + 1, stylesTable.getBorders().size());
    //id of the created border
    int borderId = (int) cellStyle.getCoreXf().getBorderId();
    assertTrue(borderId > 0);
    //check changes in the underlying xml bean
    CTBorder ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
    assertEquals(STBorderStyle.MEDIUM, ctBorder.getTop().getStyle());
    num = stylesTable.getBorders().size();
    //setting the same border multiple times should not change borderId
    for (int i = 0; i < 3; i++) {
        cellStyle.setBorderTop(BorderStyle.MEDIUM);
        assertEquals(BorderStyle.MEDIUM, cellStyle.getBorderTopEnum());
    }
    assertEquals(borderId, cellStyle.getCoreXf().getBorderId());
    assertEquals(num, stylesTable.getBorders().size());
    assertSame(ctBorder, stylesTable.getBorderAt(borderId).getCTBorder());
    //setting border to none removes the <top> element
    cellStyle.setBorderTop(BorderStyle.NONE);
    assertEquals(num, stylesTable.getBorders().size());
    borderId = (int) cellStyle.getCoreXf().getBorderId();
    ctBorder = stylesTable.getBorderAt(borderId).getCTBorder();
    assertFalse(ctBorder.isSetTop());
}
Also used : CTBorder(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder) Test(org.junit.Test)

Example 17 with CTBorder

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

the class XSSFCellStyle method setBorderTop.

/**
     * Set the type of border to use for the top border of the cell
     *
     * @param border the type of border to use
     * @since POI 3.15
     */
@Override
public void setBorderTop(BorderStyle border) {
    CTBorder ct = getCTBorder();
    CTBorderPr pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
    if (border == BorderStyle.NONE)
        ct.unsetTop();
    else
        pr.setStyle(STBorderStyle.Enum.forInt(border.getCode() + 1));
    int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme, _stylesSource.getIndexedColors()));
    _cellXf.setBorderId(idx);
    _cellXf.setApplyBorder(true);
}
Also used : CTBorder(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder) CTBorderPr(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr) XSSFCellBorder(org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder)

Example 18 with CTBorder

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

the class XSSFCellStyle method getBorderLeftEnum.

/**
     * Get the type of border to use for the left border of the cell
     * Will be removed when {@link #getBorderLeft()} returns a BorderStyle enum
     *
     * @return border type, default value is {@link org.apache.poi.ss.usermodel.BorderStyle#NONE}
     * @since POI 3.15
     */
@Override
public BorderStyle getBorderLeftEnum() {
    if (!_cellXf.getApplyBorder())
        return BorderStyle.NONE;
    int idx = (int) _cellXf.getBorderId();
    CTBorder ct = _stylesSource.getBorderAt(idx).getCTBorder();
    STBorderStyle.Enum ptrn = ct.isSetLeft() ? ct.getLeft().getStyle() : null;
    if (ptrn == null) {
        return BorderStyle.NONE;
    }
    return BorderStyle.valueOf((short) (ptrn.intValue() - 1));
}
Also used : CTBorder(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder) STBorderStyle(org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle)

Example 19 with CTBorder

use of org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder 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 20 with CTBorder

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

the class XSSFCellStyle method setTopBorderColor.

/**
     * Set the color to use for the top border as a {@link XSSFColor} value
     *
     * @param color the color to use
     */
public void setTopBorderColor(XSSFColor color) {
    CTBorder ct = getCTBorder();
    if (color == null && !ct.isSetTop())
        return;
    CTBorderPr pr = ct.isSetTop() ? ct.getTop() : ct.addNewTop();
    if (color != null)
        pr.setColor(color.getCTColor());
    else
        pr.unsetColor();
    int idx = _stylesSource.putBorder(new XSSFCellBorder(ct, _theme, _stylesSource.getIndexedColors()));
    _cellXf.setBorderId(idx);
    _cellXf.setApplyBorder(true);
}
Also used : CTBorder(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder) CTBorderPr(org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr) XSSFCellBorder(org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder)

Aggregations

CTBorder (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorder)25 CTBorder (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBorder)11 Test (org.junit.Test)10 CTBorderPr (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTBorderPr)10 CTTblBorders (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblBorders)10 CTTblPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTblPr)10 XSSFCellBorder (org.apache.poi.xssf.usermodel.extensions.XSSFCellBorder)9 STBorderStyle (org.openxmlformats.schemas.spreadsheetml.x2006.main.STBorderStyle)5 CTString (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTString)2 POIXMLException (org.apache.poi.POIXMLException)1 XmlException (org.apache.xmlbeans.XmlException)1 CTFill (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFill)1 CTFont (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFont)1 CTStylesheet (org.openxmlformats.schemas.spreadsheetml.x2006.main.CTStylesheet)1 CTP (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTP)1 CTPBdr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPBdr)1 CTPPr (org.openxmlformats.schemas.wordprocessingml.x2006.main.CTPPr)1