将表格边框设置为厚 [英] Setting Table borders to THICK

查看:163
本文介绍了将表格边框设置为厚的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个带有粗边框的表格.我已经搜索了一段时间,但似乎THICK样式不起作用.如果我选择其他样式(例如DOUBLE)就可以了,但是例如,如果我选择THIN_THICK_SMALL_GAP,则会创建两条细线.我正在使用的代码是:

I would like to create a table with thick borders. I've been searching for a while but it seems that the style THICK does not work. If I select other styles such as DOUBLE it's fine but for instance, if I select THIN_THICK_SMALL_GAP it creates two thin lines. The code I'm using is:

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

另一方面,如果我使用:

On the other hand, if I use:

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

然后它可以工作,但是我缺少桌子的外边框.

Then it works, but I'm missing the outer border of the table.

有人可以帮我吗?谢谢!

Can anyone help me with this please? Thank you!

推荐答案

不清楚为什么 XWPFTable 尚不具备此功能,但是如果我们查看

Not clear why XWPFTable does not have this already but if we look at XWPFTable.java how the setInsideHBorder works, then we can implementing this relatively easy.

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

Hint : Word itself never uses border type STBorder.THICK. Instead it uses STBorder.SINGLE because the thickness is determined by the size. That means that border type STBorder.THICK without size is also not visible. And STBorder.THICK with size 24 * 1/8 pt = 3 pt is not thicker than STBorder.SINGLE with the same size.

示例:

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

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

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

 }
}

这篇关于将表格边框设置为厚的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆