Sucy
Sucy

Reputation: 508

how to set cell setVerticalAlignment in poi word?

I want to set cell VerticalAlignment in poi word, but I found it doesn't work, this is my code. what's wrong with it?

XWPFTableCell cell = table.getRow(i).getCell(j);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);

Upvotes: 3

Views: 5020

Answers (1)

Rahul khanvani
Rahul khanvani

Reputation: 401

You cant directly set the alignment of any cell. each and every cell contains a paragraph and they contains the runs. so you also need to set their alignments. I have achieved the same problem by following method. hope it will also solve yours. thumbs up :)

private static void setHeaderRowforSingleCell(XWPFTableCell cell, String text) {
        XWPFParagraph tempParagraph = cell.getParagraphs().get(0);
        tempParagraph.setIndentationLeft(100);
        tempParagraph.setIndentationRight(100);
        tempParagraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun tempRun = tempParagraph.createRun();
        tempRun.setFontSize(10);
        tempRun.setColor("FFFFFF");
        tempRun.setText(text);
        cell.setVerticalAlignment(XWPFVertAlign.CENTER);
    }

Upvotes: 7

Related Questions