How to format the text in a XWPFTable in Apache POI

这一生的挚爱 提交于 2019-12-21 11:33:44

问题


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");

I want to format the text of the table tableTwo and tableTwoRowTwo. How can I achieve that?


回答1:


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);



回答2:


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.




回答3:


Thanks a lot and below code also worked for me. In my case I was replacing the PLACE HOLDER with another text.

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);

     }                
}


来源:https://stackoverflow.com/questions/27634991/how-to-format-the-text-in-a-xwpftable-in-apache-poi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!