gpt4 book ai didi

java - 将表格边框设置为粗

转载 作者:行者123 更新时间:2023-11-30 02:09:41 24 4
gpt4 key购买 nike

我想创建一个带有粗边框的表格。我已经搜索了一段时间,但似乎 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");

然后就可以了,但是我缺少表格的外边框。

有人可以帮我解决这个问题吗?谢谢!

最佳答案

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

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

}
}

关于java - 将表格边框设置为粗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50465457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com