当写入XWPFTable单元格时,Java Apache POI换行符将被忽略。

时间:2022-08-09 20:25:42

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:

希望有人有这方面的经验。我使用Apache POI 3.8b4来输出Word 2007格式的表。当我做类似的事情时:

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

Edit: The solution was the following:

编辑:解决方案如下:

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

3 个解决方案

#1


1  

Have you tried adding multiple Paragraphs?

你试过添加多个段落吗?

Add Paragraph

添加段落

#2


0  

Try this way :

试试这种方法:

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("A");
run.addBreak();
run.setText("B");
run.addBreak();
run.setText("C");
document.write(OutPutFilePath);

#3


0  

Try this way :

试试这种方法:

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

}

}

#1


1  

Have you tried adding multiple Paragraphs?

你试过添加多个段落吗?

Add Paragraph

添加段落

#2


0  

Try this way :

试试这种方法:

XWPFParagraph paragraph = document.createParagraph();
XWPFRun run=paragraph.createRun();
run.setText("A");
run.addBreak();
run.setText("B");
run.addBreak();
run.setText("C");
document.write(OutPutFilePath);

#3


0  

Try this way :

试试这种方法:

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

}

}