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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7477335/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:09:15  来源:igfitidea点击:

Java Apache POI newline characters are ignored when writing to XWPFTable cell

javaapache-poi

提问by ratherOCD

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();
}

采纳答案by John B

Have you tried adding multiple Paragraphs?

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

Add Paragraph

添加段落

回答by Anoop Rana

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);

回答by Arti Singh

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();

}

}

回答by Anoop Rana

XWPFRun run=paragraph.createRun();
run.setText("StringValue".trim());
run.addBreak();
document.write(OutPutFilePath);