ratherOCD
ratherOCD

Reputation: 177

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

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

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

Upvotes: 14

Views: 13913

Answers (5)

FazoM
FazoM

Reputation: 4956

Try this:

String text = ".. ... some text with line breaks... ...";
String[] textLines = text.split(System.lineSeparator());
for(String s : textLines) {
  run.setText(s);
  run.addBreak(BreakType.TEXT_WRAPPING);
}

Upvotes: 0

Anoop Rana
Anoop Rana

Reputation: 1

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

Upvotes: 0

Arti Singh
Arti Singh

Reputation: 1

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

}

Upvotes: 0

Anoop Rana
Anoop Rana

Reputation: 31

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

Upvotes: 3

John B
John B

Reputation: 32969

Have you tried adding multiple Paragraphs?

Add Paragraph

Upvotes: 0

Related Questions