I need a border to my XWPFTableCell like below table.
**XXX Technologies**
__________________
|Name |Gender |Salary |
__________________
Raji FeMale 24000
Ravi Male 06790
I need a border to my XWPFTableCell like below table.
**XXX Technologies**
__________________
|Name |Gender |Salary |
__________________
Raji FeMale 24000
Ravi Male 06790
CTTc ctTc = cell.getCTTc();
// here is need to change...
CTTcPr tcPr = ctTc.addNewTcPr();
CTTcBorders border = tcPr.addNewTcBorders();
border.addNewRight().setVal(STBorder.SINGLE);
This method will allow you to set the borders of table to your desired color as well.
private static void setTableBorderColor(XWPFTable table, String color) {
table.getCTTbl().getTblPr().getTblBorders().getBottom().setColor(color);
table.getCTTbl().getTblPr().getTblBorders().getTop().setColor(color);
table.getCTTbl().getTblPr().getTblBorders().getLeft().setColor(color);
table.getCTTbl().getTblPr().getTblBorders().getRight().setColor(color);
table.getCTTbl().getTblPr().getTblBorders().getInsideH().setColor(color);
table.getCTTbl().getTblPr().getTblBorders().getInsideV().setColor(color);
table.getCTTbl().getTblPr().getTblBorders().getRight().setSz(BigInteger.valueOf(4));
table.getCTTbl().getTblPr().getTblBorders().getTop().setSz(BigInteger.valueOf(4));
table.getCTTbl().getTblPr().getTblBorders().getLeft().setSz(BigInteger.valueOf(4));
table.getCTTbl().getTblPr().getTblBorders().getBottom().setSz(BigInteger.valueOf(4));
table.getCTTbl().getTblPr().getTblBorders().getInsideH().setSz(BigInteger.valueOf(4));
table.getCTTbl().getTblPr().getTblBorders().getInsideV().setSz(BigInteger.valueOf(4));
}
You can add border to your cell like below
CTTc ctTc = cell.getCTTc();
CTTcPr tcPr = ctTc.getTcPr();
CTTcBorders border = tcPr.addNewTcBorders();
border.addNewBottom().setVal(STBorder.SINGLE);
border.addNewRight().setVal(STBorder.SINGLE);
border.addNewLeft().setVal(STBorder.SINGLE);
border.addNewTop().setVal(STBorder.SINGLE);
You can use this helper function to style entire table (adapted from Rahul khanvani):
public static void tableSetBorders(
XWPFTable table,
STBorder.Enum borderType,
int size,
int space,
String hexColor) {
table.getCTTbl().getTblPr().getTblBorders().getBottom().setColor(hexColor);
table.getCTTbl().getTblPr().getTblBorders().getTop().setColor(hexColor);
table.getCTTbl().getTblPr().getTblBorders().getLeft().setColor(hexColor);
table.getCTTbl().getTblPr().getTblBorders().getRight().setColor(hexColor);
table.getCTTbl().getTblPr().getTblBorders().getInsideH().setColor(hexColor);
table.getCTTbl().getTblPr().getTblBorders().getInsideV().setColor(hexColor);
table.getCTTbl().getTblPr().getTblBorders().getRight().setSz(BigInteger.valueOf(size));
table.getCTTbl().getTblPr().getTblBorders().getTop().setSz(BigInteger.valueOf(size));
table.getCTTbl().getTblPr().getTblBorders().getLeft().setSz(BigInteger.valueOf(size));
table.getCTTbl().getTblPr().getTblBorders().getBottom().setSz(BigInteger.valueOf(size));
table.getCTTbl().getTblPr().getTblBorders().getInsideH().setSz(BigInteger.valueOf(size));
table.getCTTbl().getTblPr().getTblBorders().getInsideV().setSz(BigInteger.valueOf(size));
table.getCTTbl().getTblPr().getTblBorders().getBottom().setVal(borderType);
table.getCTTbl().getTblPr().getTblBorders().getTop().setVal(borderType);
table.getCTTbl().getTblPr().getTblBorders().getLeft().setVal(borderType);
table.getCTTbl().getTblPr().getTblBorders().getRight().setVal(borderType);
table.getCTTbl().getTblPr().getTblBorders().getInsideH().setVal(borderType);
table.getCTTbl().getTblPr().getTblBorders().getInsideV().setVal(borderType);
}