【问题标题】:Apache poi xwpf table alignmentApache poi xwpf 表对齐
【发布时间】:2018-02-03 05:32:48
【问题描述】:

我正在使用以下代码在 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*/

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

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

【问题讨论】:

标签: apache-poi xwpf


【解决方案1】:

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

public void setTableAlignment(XWPFTable table, STJc.Enum justification) {
    CTTblPr tblPr = table.getCTTbl().getTblPr();
    CTJc jc = (tblPr.isSetJc() ? tblPr.getJc() : tblPr.addNewJc());
    jc.setVal(justification);
}

【讨论】:

    【解决方案2】:

    这段代码对我有用:

    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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-02
      • 1970-01-01
      • 2014-04-11
      • 1970-01-01
      • 1970-01-01
      • 2016-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多