如何在 Apache POI 中格式化 XWPFTable 中的文本 [英] How to format the text in a XWPFTable in Apache POI
问题描述
我使用 Apache POI 在 word 中创建了一个 XWPFTable
.现在表格正确地出现在列中的文本中.现在我想格式化表格中的文本以及大小、字体等.我该怎么做?我看到的是每个技巧都与运行选项相关联.但我想要的是TableRow
.看看我到目前为止做了什么:
I have created a XWPFTable
in word using Apache POI. Now the table is coming out properly with text in the column. Now I want to format the text in the table along with size, font etc. How can I do that? What I am seeing is that every trick is associated with the run option. But what I want is in TableRow
. See what I have done so far:
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");
我想格式化表格tableTwo
和tableTwoRowTwo
的文本.我怎样才能做到这一点?
I want to format the text of the table tableTwo
and tableTwoRowTwo
. How can I achieve that?
推荐答案
在单元格中添加段落.
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();
}
通常这会在单元格中再添加一段,但该单元格已经有一段了.所以先删除单元格中的段落,然后再添加新的段落,否则结果将是新段落之前的空行.
Usually this would add one more paragraph in the cell, but the cell already has one. So first remove the paragraph in the cell and then add new one, otherwise the result would be empty row before the new paragraph.
rowOne.getCell(0).removeParagraph(0);
这篇关于如何在 Apache POI 中格式化 XWPFTable 中的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!