-4

I need a border to my XWPFTableCell like below table.

        **XXX Technologies**

__________________
|Name |Gender |Salary |
__________________
Raji FeMale 24000
Ravi Male 06790

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
S Kumar
  • 11
  • 1
  • 4

4 Answers4

6
CTTc ctTc = cell.getCTTc(); 
// here is need to change... 
CTTcPr tcPr = ctTc.addNewTcPr();
CTTcBorders border = tcPr.addNewTcBorders();
border.addNewRight().setVal(STBorder.SINGLE);
Pawan Kumar
  • 65
  • 1
  • 5
2

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));
}
Alexander
  • 2,925
  • 3
  • 33
  • 36
Rahul khanvani
  • 373
  • 3
  • 13
1

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); 
Siva kumar
  • 215
  • 4
  • 13
0

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);
}
zoran
  • 943
  • 11
  • 22