1

我正在使用以下代码在 xwpf 中的 Apache Poi 中创建一个表:

          XWPFTable table = doc.createTable(Countrows1+1,4);
          table.getCTTbl().getTblPr().unsetTblBorders();
          table.setInsideHBorder(XWPFTable.XWPFBorderType.DASHED, 0,0,null);
          table.setInsideVBorder(XWPFTable.XWPFBorderType.DASHED,0,0, null);
          /* table header */

          table.getRow(0).getCell(0).setText("Column1");
          table.getRow(0).getCell(1).setText("Column2");
          table.getRow(0).getCell(2).setText("Column3");
          table.getRow(0).getCell(3).setText("Column");

          /* other rows to be populed here*/

我找不到任何方法可以将表格对齐到页面的左侧、中心或右侧。有没有办法做到这一点?

第一行的内容也应该是粗体。有什么方法可以将标题设置为粗体吗?

4

2 回答 2

2

您可以使用以下功能对齐表格

public void setTableAlignment(XWPFTable table, STJc.Enum justification) {
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc());
    jc.setVal(justification);
}
于 2015-08-22T19:06:08.137 回答
1

这段代码对我有用:

public void setTableAlign(XWPFTable table,ParagraphAlignment align) {
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc());
    STJc.Enum en = STJc.Enum.forInt(align.getValue());
    jc.setVal(en);
}

你这样调用方法:

XWPFTable table = documento.createTable();
setTableAlign(table, ParagraphAlignment.CENTER);
于 2018-02-02T21:32:48.287 回答