根据 \n 进行分割,取第一个 run,并将分割后的元素作为其他 run拼起来
run.addBreak(); // 换行
1 /** 2 * 匹配单元格内容\n 替换为换行 3 * @param cell 4 */ 5 private void addBreakInCell(XWPFTableCell cell) { 6 if(cell.getText() != null && cell.getText().contains("\n")) { 7 for (XWPFParagraph paragraph : cell.getParagraphs()) { 8 for (XWPFRun run : paragraph.getRuns()) { 9 if(run.getText(0)!= null && run.getText(0).contains("\n")) { 10 String[] lines = run.getText(0).split("\n"); 11 if(lines.length > 0) { 12 // set first line into XWPFRun 13 run.setText(lines[0], 0); 14 for(int i=1;i<lines.length;i++){ 15 // add break and insert new text 16 run.addBreak(); 17 run.setText(lines[i]); 18 } 19 } 20 } 21 } 22 } 23 } 24 }
标签:run,换行,单元格,lines,getText,cell,POI From: https://www.cnblogs.com/gouzhu/p/18120976