From f5c12773bd166578ed00f447ca24323d850475fe Mon Sep 17 00:00:00 2001 From: jueyue Date: Wed, 13 Jan 2016 11:27:34 +0800 Subject: [PATCH] =?UTF-8?q?word=20=E6=8D=A2=E8=A1=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../poi/util/PoiPublicUtil.java | 19 +++++++++++++++++++ .../poi/word/parse/ParseWord07.java | 2 +- .../word/parse/excel/ExcelEntityParse.java | 3 ++- .../poi/word/parse/excel/ExcelMapParse.java | 7 +++++-- 4 files changed, 27 insertions(+), 4 deletions(-) diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java index a33aaa4..e42018a 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/util/PoiPublicUtil.java @@ -47,6 +47,7 @@ import org.apache.poi.xssf.usermodel.XSSFShape; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.apache.poi.xwpf.usermodel.XWPFDocument; +import org.apache.poi.xwpf.usermodel.XWPFRun; import org.jeecgframework.poi.excel.annotation.Excel; import org.jeecgframework.poi.excel.annotation.ExcelCollection; import org.jeecgframework.poi.excel.annotation.ExcelEntity; @@ -457,5 +458,23 @@ public final class PoiPublicUtil { } return defalut; } + + + /** + * 支持换行操作 + * @param currentRun + * @param currentText + */ + public static void setWordText(XWPFRun currentRun, String currentText) { + if(StringUtils.isNotBlank(currentText)){ + String[] tempArr = currentText.split("\r\n"); + for (int i = 0, le = tempArr.length - 1; i < le; i++) { + currentRun.setText(tempArr[i], i); + currentRun.addBreak(); + } + currentRun.setText(tempArr[tempArr.length - 1], tempArr.length - 1); + } + } + } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/ParseWord07.java b/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/ParseWord07.java index 2dccc7e..3d7c908 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/ParseWord07.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/ParseWord07.java @@ -94,7 +94,7 @@ public class ParseWord07 { addAnImage((WordImageEntity) obj, currentRun); } else { currentText = obj.toString(); - currentRun.setText(currentText, 0); + PoiPublicUtil.setWordText(currentRun, currentText); } for (int k = 0; k < runIndex.size(); k++) { paragraph.getRuns().get(runIndex.get(k)).setText("", 0); diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelEntityParse.java b/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelEntityParse.java index 1b0125e..5487462 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelEntityParse.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelEntityParse.java @@ -183,8 +183,9 @@ public class ExcelEntityParse extends ExportBase { 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()); } else { - row.createCell().setText(value == null ? "" : value.toString()); + PoiPublicUtil.setWordText(row.createCell().addParagraph().createRun(),value == null ? "" : value.toString()); } } diff --git a/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelMapParse.java b/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelMapParse.java index 28362e4..98c9be0 100644 --- a/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelMapParse.java +++ b/easypoi-base/src/main/java/org/jeecgframework/poi/word/parse/excel/ExcelMapParse.java @@ -20,9 +20,11 @@ import static org.jeecgframework.poi.util.PoiElUtil.*; import java.util.List; import java.util.Map; +import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFTable; import org.apache.poi.xwpf.usermodel.XWPFTableCell; import org.apache.poi.xwpf.usermodel.XWPFTableRow; +import org.jeecgframework.poi.util.PoiPublicUtil; import com.google.common.collect.Maps; @@ -79,12 +81,13 @@ public final class ExcelMapParse { tempMap.put("t", obj); for (cellIndex = 0; cellIndex < currentRow.getTableCells().size(); cellIndex++) { String val = eval(params[cellIndex], tempMap).toString(); - currentRow.getTableCells().get(cellIndex).setText(val); + currentRow.getTableCells().get(cellIndex).setText(""); + PoiPublicUtil.setWordText(currentRow.getTableCells().get(cellIndex).addParagraph().createRun(),val); } for (; cellIndex < params.length; cellIndex++) { String val = eval(params[cellIndex], tempMap).toString(); - currentRow.createCell().setText(val); + PoiPublicUtil.setWordText(currentRow.createCell().addParagraph().createRun(),val); } }