写入 XWPFTable 单元格时忽略 Java Apache POI 换行符

问题描述:

希望有人可能有这方面的经验.我正在使用 Apache POI 3.8b4 以 Word 2007 格式输出表格.当我做类似以下的事情时:

Hoping someone might have some experience with this. I'm using Apache POI 3.8b4 to output a table in Word 2007 format. When I do something similar to the following:

XWPFTableRow row = table.getRow(0);
String text = "A\nB\nC\nD";
row.getCell(i).setText(text);

我的所有换行符都在表格单元格的输出中被忽略

all of my line breaks are ignored in the output in the table cell looks like

A B C D

有谁知道如何让它正确显示为

Does anyone have any idea how to get it to properly display as

A
B
C
D

解决方案如下:

XWPFParagraph para = row.getCell(i).getParagraphs().get(0);
for(String text : myStrings){
    XWPFRun run = para.createRun();
    run.setText(text.trim());
    run.addBreak();
}

您是否尝试过添加多个段落?

Have you tried adding multiple Paragraphs?

添加段落