From 1e651a4c54522285998dfe0109cef43cc465a9ba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=80=9D=E6=95=8F?= Date: Fri, 2 Nov 2018 22:45:30 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3word=E5=AF=BC=E5=87=BA?= =?UTF-8?q?=E8=A1=A8=E6=A0=BC=E5=A4=9A=E5=87=BA=E7=9A=84=E6=8D=A2=E8=A1=8C?= =?UTF-8?q?=E7=AC=A6=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../word/parse/excel/ExcelEntityParse.java | 32 ++++++++++++++++--- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java b/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java index 96b07b3..344d795 100644 --- a/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java +++ b/easypoi-base/src/main/java/cn/afterturn/easypoi/word/parse/excel/ExcelEntityParse.java @@ -25,6 +25,8 @@ import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.builder.ReflectionToStringBuilder; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; +import org.apache.poi.xwpf.usermodel.XWPFRun; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; @@ -198,14 +200,36 @@ public class ExcelEntityParse extends ExportCommonService { private void setCellValue(XWPFTableRow row, Object value, int cellNum) { if (row.getCell(cellNum++) != null) { row.getCell(cellNum - 1).setText(value == null ? "" : value.toString()); - PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(), - value == null ? "" : value.toString()); + setWordText(row, value); } else { - PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(), - value == null ? "" : value.toString()); + setWordText(row, value); } } + /** + * 解决word导出表格多出的换行符问题 + * @param row + * @param value + */ + private void setWordText(XWPFTableRow row, Object value) { + XWPFTableCell cell = row.createCell(); + List paragraphs = cell.getParagraphs(); + XWPFParagraph paragraph = null; + XWPFRun run = null; + if(paragraphs != null && paragraphs.size() > 0) { + paragraph = paragraphs.get(0); + } else { + paragraph = row.createCell().addParagraph(); + } + List runs = paragraph.getRuns(); + if(runs != null && runs.size() > 0) { + run = runs.get(0); + } else { + run = paragraph.createRun(); + } + PoiPublicUtil.setWordText(run, value == null ? "" : value.toString()); + } + /** * 对导出序列进行排序和塞选 *