From 3190e8ad5a2e30d11b331a833a1df3f4b93f0d1e Mon Sep 17 00:00:00 2001 From: liyawei Date: Thu, 15 Sep 2022 09:03:08 +0800 Subject: [PATCH] =?UTF-8?q?=E8=AE=A4=E8=AF=81-=E4=B8=8A=E6=8A=A5=E5=BA=93-?= =?UTF-8?q?word=E8=87=AA=E5=AE=9A=E4=B9=89=E5=AF=BC=E5=87=BA:=20=E8=A7=A3?= =?UTF-8?q?=E5=86=B3=E8=A1=A8=E6=A0=BC=E4=B8=8D=E8=AF=86=E5=88=AB=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../impl/ParamsReportDetailEOServiceImpl.java | 6 +- .../modules/project/util/Docx4jUtils.java | 64 +++++++++++++++++++ .../jero/modules/project/util/WordUtil.java | 7 +- 3 files changed, 74 insertions(+), 3 deletions(-) diff --git a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java index 4879c80b5..3ecc063ea 100644 --- a/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java +++ b/jero-boot/jero-boot-module-certification/src/main/java/com/jero/modules/cert/report/service/impl/ParamsReportDetailEOServiceImpl.java @@ -30,6 +30,7 @@ import com.jero.modules.cert.template.enums.ParamsIsMustEnum; import com.jero.modules.ocr.util.LineHumpUtil; import com.jero.modules.oss.entity.OSSFile; import com.jero.modules.oss.service.IOSSFileService; +import com.jero.modules.project.util.Docx4jUtils; import com.jero.modules.project.util.ExcelUtil; import com.jero.modules.project.util.WordUtil; import com.jero.modules.split.common.ReadExcel; @@ -48,6 +49,7 @@ import org.docx4j.Docx4J; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.SpreadsheetMLPackage; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; +import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.mock.web.MockMultipartFile; @@ -785,6 +787,8 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl> fileListAll = (List>) allParamsInfoList.get(2).get("fileListAll"); @@ -838,7 +842,7 @@ public class ParamsReportDetailEOServiceImpl extends ServiceImpl 0) { + sb.append("#"); + } + sb.append(s1); + appender.append(sb.toString()); + continue; + } + //被分离的数据一般都是'${'这样被分开 + //匹配以'$'开头的变量找到'}' 得到索引位置 + int i1 = s1.indexOf("}"); + //先切割得到这个完整体 + String substr = s1.substring(0, i1 + 1); + //把变量追加到StringBuilder + sb.append(substr.replaceAll("\n","").replaceAll("<[^>]+>", "")); + //再将标签数据追加到StringBuilder包裹变量 + sb.append(s1.substring(i1 + 1)); + + appender.append(sb.toString()); + } + + return appender.toString(); + } + /** * 清扫 docx4j 模板变量字符,通常以${variable}形式 *

diff --git a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/WordUtil.java b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/WordUtil.java index 4d66e753e..b492d8057 100644 --- a/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/WordUtil.java +++ b/jero-boot/jero-boot-modules/src/main/java/com/jero/modules/project/util/WordUtil.java @@ -59,6 +59,11 @@ public class WordUtil { documentPart.variableReplace((HashMap) map); } + public static void replaceVariableWithOutClear(WordprocessingMLPackage wordMLPackage, Map map) throws Exception { + MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart(); + documentPart.variableReplace((HashMap) map); + } + public static void replaceTable(String templatePath, int tableNum, int rowNum, List> dataList, String outPath) throws Exception { WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(new File(templatePath)); MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); @@ -203,7 +208,6 @@ public class WordUtil { public static void replacePictureBatch(WordprocessingMLPackage wordMLPackage, List> picList) throws Exception { try { MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); - Docx4jUtils.cleanDocumentPart(mainDocumentPart); Document wmlDoc = (Document)mainDocumentPart.getJaxbElement(); Body body = wmlDoc.getBody(); List textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符 @@ -241,7 +245,6 @@ public class WordUtil { */ public static Map filterParams(WordprocessingMLPackage wordMLPackage, Map params) throws Exception { MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart(); - Docx4jUtils.cleanDocumentPart(mainDocumentPart); Document wmlDoc = (Document)mainDocumentPart.getJaxbElement(); Body body = wmlDoc.getBody(); List textList = getAllPlaceholderElementFromObject(body); // 获取文档中所有占位符