diff --git a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java index f3044af3fd..8537df186d 100644 --- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java +++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFTableCell.java @@ -421,7 +421,28 @@ public class XWPFTableCell implements IBody, ICell { return text.toString(); } + /** + * Set the text of the cell to the passed string, replacing previous content. Up until POI 5.2.3, this method appended the text, which is now done + * by {@link XWPFTableCell#appendText(String)}. + * + * @param text The text to replace the cell content with + */ public void setText(String text) { + XWPFParagraph par = paragraphs.isEmpty() ? addParagraph() : paragraphs.get(0); + while (!par.runsIsEmpty()) { + par.removeRun(0); + } + par.createRun().setText(text); + } + + /** + * Append the passed string to the cell content. + * This was the behaviour of {@link XWPFTableCell#setText(String)} before POI 5.2.4 + * + * @param text The text to append to the cells content. + * @since POI 5.2.4 + */ + public void appendText(String text) { XWPFParagraph par = paragraphs.isEmpty() ? addParagraph() : paragraphs.get(0); par.createRun().setText(text); } diff --git a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java index 8afaaecb2d..7b20827b78 100644 --- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java +++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/usermodel/TestXWPFBugs.java @@ -329,6 +329,17 @@ class TestXWPFBugs { } } + @Test + void bug66988() throws IOException { + try (XWPFDocument document = XWPFTestDataSamples.openSampleDocument("Bug66988.docx")) { + XWPFTableCell cell = document.getTableArray(0).getRow(0).getCell(0); + cell.appendText("World"); + assertEquals("HelloWorld", cell.getText()); + cell.setText("FooBar"); + assertEquals("FooBar", cell.getText()); + } + } + private static void addNumberingWithAbstractId(XWPFNumbering documentNumbering, int id){ // create a numbering scheme CTAbstractNum cTAbstractNum = CTAbstractNum.Factory.newInstance(); diff --git a/test-data/document/Bug66988.docx b/test-data/document/Bug66988.docx new file mode 100644 index 0000000000..4b7a781bc3 Binary files /dev/null and b/test-data/document/Bug66988.docx differ