How to format text in XWPFTable in Apache POI

I created XWPFTable in a word using Apache POI. Now the table fits with the text in the column. Now I want to format the text in the table along with the size, font, etc. How can i do this? I see that all the tricks are related to the run parameter. But I want it in the 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 this ??

+4
source share
3 answers

Add a paragraph to 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();
    }

This usually adds another paragraph to the cell, but the cell already has one. Therefore, first delete the paragraph in the cell, and then add a new one, otherwise the result will be an empty line before the new paragraph.

rowOne.getCell(0).removeParagraph(0);
+12

.

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

.

+3

Thanks a lot, and the code below worked for me too. In my case, I replaced the LOCATOR 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);

     }                
}
0
source

Source: https://habr.com/ru/post/1568866/


All Articles