如何格式化Apache POI中的XWPFTable中的文本

luc*_*fer 7 java apache-poi

XWPFTable使用Apache POI 创建了一个单词.现在,表格正确地显示在列中的文本.现在我想格式化表格中的文字以及大小,字体等.我该怎么做?我所看到的是每个技巧都与运行选项相关联.但我想要的是TableRow.看看到目前为止我做了什么:

XWPFTable tableTwo = document.createTable();
XWPFTableRow tableTwoRowOne = tableTwo.getRow(0);
tableTwo.getCTTbl().getTblPr().unsetTblBorders();
tableTwoRowOne.getCell(0).setText("No Match – Location: ");
tableTwoRowOne.addNewTableCell().setText("Prospect has expressed unwillingness to relocate or is based out of area where commute is not feasible");

XWPFTableRow tableTwoRowTwo = tableTwo.createRow();
tableTwoRowTwo.getCell(0).setText("No Match – Scalability: ");
tableTwoRowTwo.getCell(1).setText("Prospect’s recent organizational size, structure, and complexity is not scalable to client’s environment");
Run Code Online (Sandbox Code Playgroud)

我想格式化表的文本tableTwotableTwoRowTwo.我怎样才能做到这一点?

mak*_*asi 19

在单元格中添加段落.

XWPFTableRow rowOne = table.getRow(0);
                XWPFParagraph paragraph = rowOne.getCell(0).addParagraph();
                setRun(paragraph.createRun() , "Calibre LIght" , 10, "2b5079" , "Some string" , false, false);

private static void setRun (XWPFRun run , String fontFamily , int fontSize , String colorRGB , String text , boolean bold , boolean addBreak) {
        run.setFontFamily(fontFamily);
        run.setFontSize(fontSize);
        run.setColor(colorRGB);
        run.setText(text);
        run.setBold(bold);
        if (addBreak) run.addBreak();
    }
Run Code Online (Sandbox Code Playgroud)

通常这会在单元格中添加一个段落,但单元格已经有一个.因此,首先删除单元格中的段落,然后添加新段落,否则结果将在新段落之前为空行.

rowOne.getCell(0).removeParagraph(0);
Run Code Online (Sandbox Code Playgroud)


小智 5

我找到了一个简单的解决方案。

XWPFTable table = document.createTable();
XWPFTableRow row = table.insertNewTableRow(0);
XWPFRun run = row.addNewTableCell().addParagraph().createRun();
run.setBold(true);run.setText("Your Text");
Run Code Online (Sandbox Code Playgroud)

您可以添加任何其他文本格式函数来运行对象。