gpt4 book ai didi

java - 如何在 XWPFTableCell 上设置自定义背景颜色?

转载 作者:行者123 更新时间:2023-12-02 12:24:19 28 4
gpt4 key购买 nike

希望有人能帮忙解决这个问题。

我正在尝试使用 apache poi 生成 docx 文档,但现在我遇到了如何在表格列或单元格中设置背景颜色的问题?

我正在努力实现这样的目标。 here

这就是我到目前为止所拥有的。 here

下面是我的代码。

public static void main(String [] args){
XWPFDocument doc = new XWPFDocument();
XWPFTable table = doc.createTable(3, 4);
fillHeader(table);
mergeCellsHorizontally(table, 0, 0, 1);
mergeCellsVertically(table, 2, 0, 1);
mergeCellsVertically(table, 3, 0, 1);
}

private static void fillHeader(XWPFTable table) {
XWPFTableRow row = table.getRow(0);
row.getCell(0).setText("Column 1");
row.getCell(2).setText("Column 2");
row.getCell(3).setText("Column 3");
XWPFTableRow row1 = table.getRow(1);
row1.getCell(0).setText("Col 1 Row 1");
row1.getCell(1).setText("Col 2 Row 1");
}

private static void mergeCellsHorizontally(XWPFTable table, int row, int
fromCol, int toCol) {
for (int cellIndex = fromCol; cellIndex <= toCol; cellIndex++) {
XWPFTableCell cell = table.getRow(row).getCell(cellIndex);
if (cellIndex == fromCol) {ue
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewHMerge().setVal(STMerge.CONTINUE);
}
}
}

private static void mergeCellsVertically(XWPFTable table, int col, int
fromRow, int toRow) {
for (int rowIndex = fromRow; rowIndex <= toRow; rowIndex++) {
XWPFTableCell cell = table.getRow(rowIndex).getCell(col);
if (rowIndex == fromRow) {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.RESTART);
} else {
cell.getCTTc().addNewTcPr().addNewVMerge().setVal(STMerge.CONTINUE);
}
}
}

最佳答案

设置单元格的背景颜色,如下所示:

cell.getCTTc().addNewTcPr().addNewShd().setFill("cccccc");

颜色应使用十六进制,如cccccc

关于java - 如何在 XWPFTableCell 上设置自定义背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45559120/

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