java 如何在 Apache POI 中格式化 XWPFTable 中的文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27634991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to format the text in a XWPFTable in Apache POI
提问by lucifer
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
使用 Apache POI创建了一个in word。现在表格正确地出现在列中的文本中。现在我想格式化表格中的文本以及大小、字体等。我该怎么做?我所看到的是每个技巧都与运行选项相关联。但我想要的是在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");
I want to format the text of the table tableTwo
and tableTwoRowTwo
. How can I achieve that?
我想格式化表格的文本tableTwo
和tableTwoRowTwo
. 我怎样才能做到这一点?
回答by makkasi
Add paragraph in the cell.
在单元格中添加段落。
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);
回答by Sachin Dandavati
I found a simple solution.
我找到了一个简单的解决方案。
XWPFTable table = document.createTable();
XWPFTableRow row = table.insertNewTableRow(0);
XWPFRun run = row.addNewTableCell().addParagraph().createRun();
run.setBold(true);run.setText("Your Text");
You can add any other text format functions to run object.
您可以添加任何其他文本格式函数来运行对象。
回答by Hitesh Patel
Thanks a lot and below code also worked for me. In my case I was replacing the PLACE HOLDER with another text.
非常感谢,下面的代码也对我有用。就我而言,我用另一个文本替换了 PLACE HOLDER。
for (XWPFTableCell cell : cells) {
String cellTextString = cell.getText();
if (cellTextString != null && cellTextString.contains(placeholder)) {
cellTextString = cellTextString.replace(placeholder,waterMarkText);
cell.removeParagraph(0);
XWPFParagraph addParagraph = cell.addParagraph();
XWPFRun run = addParagraph.createRun();
run.setFontFamily("Calibri");
run.setFontSize(10);
run.setText(cellTextString);
}
}