如何在 poi word 中设置单元格 setVerticalAlignment?

Suc*_*ucy 2 apache-poi

我想在 poi word 中设置单元格 VerticalAlignment,但我发现它不起作用,这是我的代码。它出什么问题了?

XWPFTableCell cell = table.getRow(i).getCell(j);
cell.setVerticalAlignment(XWPFTableCell.XWPFVertAlign.CENTER);
Run Code Online (Sandbox Code Playgroud)

小智 6

您不能直接设置任何单元格的对齐方式。每个单元格都包含一个段落,它们包含运行。所以你还需要设置它们的对齐方式。我通过以下方法解决了同样的问题。希望它也能解决你的问题。竖起大拇指 :)

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);
    }
Run Code Online (Sandbox Code Playgroud)