首页 > 解决方案 > 将表格边框设置为厚

问题描述

我想创建一个带有粗边框的表格。我一直在寻找一段时间,但似乎 THICK 风格不起作用。如果我选择其他样式,例如 DOUBLE,那很好,但例如,如果我选择 THIN_THICK_SMALL_GAP,它会创建两条细线。我正在使用的代码是:

CTTblPr tblpro = table.getCTTbl().getTblPr();

CTTblBorders borders = tblpro.addNewTblBorders();
borders.addNewBottom().setVal(STBorder.THICK);
borders.addNewLeft().setVal(STBorder.THICK);
borders.addNewRight().setVal(STBorder.THICK);
borders.addNewTop().setVal(STBorder.THICK);
borders.addNewInsideH().setVal(STBorder.THICK);
borders.addNewInsideV().setVal(STBorder.THICK);

另一方面,如果我使用:

table.setInsideHBorder(XWPFTable.XWPFBorderType.THICK, 4, 0, "000000");
table.setInsideVBorder(XWPFTable.XWPFBorderType.THICK, 4, 0, "000000");

然后它可以工作,但我错过了表格的外边框。

任何人都可以帮我解决这个问题吗?谢谢!

标签: javaapache-poi

解决方案


不清楚为什么XWPFTable还没有这个,但是如果我们看看XWPFTable.java是如何setInsideHBorder工作的,那么我们可以相对容易地实现这个。

提示:Word它本身从不使用边框类型STBorder.THICK。相反,它使用STBorder.SINGLE,因为厚度是由大小决定的。这意味着STBorder.THICK没有大小的边框类型也不可见。并且STBorder.THICK尺寸为 24 * 1/8 pt = 3 pt 并不比STBorder.SINGLE相同尺寸的厚。

例子:

import java.io.FileOutputStream;

import org.apache.poi.xwpf.usermodel.*;
import org.apache.poi.xwpf.usermodel.XWPFTable.XWPFBorderType;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.util.EnumMap;

import java.math.BigInteger;

public class CreateWordTableBorders {

 private static EnumMap<XWPFBorderType, STBorder.Enum> xwpfBorderTypeMap;
 static {
  // populate enum map
  xwpfBorderTypeMap = new EnumMap<XWPFBorderType, STBorder.Enum>(XWPFBorderType.class);
  xwpfBorderTypeMap.put(XWPFBorderType.NIL, STBorder.Enum.forInt(STBorder.INT_NIL));
  xwpfBorderTypeMap.put(XWPFBorderType.NONE, STBorder.Enum.forInt(STBorder.INT_NONE));
  xwpfBorderTypeMap.put(XWPFBorderType.SINGLE, STBorder.Enum.forInt(STBorder.INT_SINGLE));
  xwpfBorderTypeMap.put(XWPFBorderType.THICK, STBorder.Enum.forInt(STBorder.INT_THICK));
  xwpfBorderTypeMap.put(XWPFBorderType.DOUBLE, STBorder.Enum.forInt(STBorder.INT_DOUBLE));
  xwpfBorderTypeMap.put(XWPFBorderType.DOTTED, STBorder.Enum.forInt(STBorder.INT_DOTTED));
  xwpfBorderTypeMap.put(XWPFBorderType.DASHED, STBorder.Enum.forInt(STBorder.INT_DASHED));
  xwpfBorderTypeMap.put(XWPFBorderType.DOT_DASH, STBorder.Enum.forInt(STBorder.INT_DOT_DASH));
 }

 private enum BorderPosition {
  TOP, BOTTOM, LEFT, RIGHT
 }

 private static void setTableBorder(BorderPosition position, XWPFTable table, XWPFBorderType type, 
  int size, int space, String rgbColor) {

  CTTblPr tblPr = (table.getCTTbl().getTblPr() != null) ? table.getCTTbl().getTblPr() : table.getCTTbl().addNewTblPr();
  CTTblBorders ctb = tblPr.isSetTblBorders() ? tblPr.getTblBorders() : tblPr.addNewTblBorders();
  CTBorder b = null;
  switch (position) {
   case TOP:
   b = ctb.isSetTop() ? ctb.getTop() : ctb.addNewTop();
   break;
   case BOTTOM:
   b = ctb.isSetBottom() ? ctb.getBottom() : ctb.addNewBottom();
   break;
   case LEFT:
   b = ctb.isSetLeft() ? ctb.getLeft() : ctb.addNewLeft();
   break;
   case RIGHT:
   b = ctb.isSetRight() ? ctb.getRight() : ctb.addNewRight();
   break;
  }
  b.setVal(xwpfBorderTypeMap.get(type));
  b.setSz(BigInteger.valueOf(size));
  b.setSpace(BigInteger.valueOf(space));
  b.setColor(rgbColor);
 }

 public static void main(String[] args) throws Exception {

  XWPFDocument document = new XWPFDocument();

  XWPFParagraph paragraph = document.createParagraph();

  XWPFTable table = document.createTable(3, 3);
  //create CTTblGrid for this table with widths of the 3 columns. 
  //necessary for Libreoffice/Openoffice to accept the column widths.
  //values are in unit twentieths of a point (1/1440 of an inch)
  //first column = 1 inches width
  table.getCTTbl().addNewTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*1440));
  //other columns (2 in this case) also each 1 inches width
  for (int col = 1 ; col < 3; col++) {
   table.getCTTbl().getTblGrid().addNewGridCol().setW(BigInteger.valueOf(1*1440));
  }

  for (int col = 0; col < 3; col++) {
   table.getRow(0).getCell(col).setText("Column " + (col+1));
   if (table.getRow(1).getCell(col).getParagraphs().size() ==0) table.getRow(1).getCell(col).addParagraph();
   if (table.getRow(2).getCell(col).getParagraphs().size() ==0) table.getRow(2).getCell(col).addParagraph();
  }

  setTableBorder(BorderPosition.TOP, table, XWPFBorderType.SINGLE, 24/*unit 1/8 pt*/, 0, "0000FF");
  setTableBorder(BorderPosition.BOTTOM, table, XWPFBorderType.SINGLE, 24/*unit 1/8 pt*/, 0, "0000FF");
  setTableBorder(BorderPosition.LEFT, table, XWPFBorderType.SINGLE, 24/*unit 1/8 pt*/, 0, "0000FF");
  setTableBorder(BorderPosition.RIGHT, table, XWPFBorderType.SINGLE, 24/*unit 1/8 pt*/, 0, "0000FF");

  table.setInsideHBorder(XWPFTable.XWPFBorderType.DASHED, 8/*unit 1/8 pt*/, 0, "000000");
  table.setInsideVBorder(XWPFTable.XWPFBorderType.DASHED, 8/*unit 1/8 pt*/, 0, "000000");

  paragraph = document.createParagraph();

  FileOutputStream out = new FileOutputStream("CreateWordTableBorders.docx");
  document.write(out);
  out.close();
  document.close();

 }
}

推荐阅读