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());
}
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);
}
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));
}
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");
}
}
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);
}
Aggregations