From fba68d129dd022883186d6182811a6ef92568505 Mon Sep 17 00:00:00 2001 From: zhijie <1003392067@qq.com> Date: 星期六, 10 七月 2021 16:16:39 +0800 Subject: [PATCH] 支持多角色,支持单列表格多个变量替换 --- src/main/java/com/ots/common/utils/poi/WordUtil.java | 36 +++++++++++++++++++++++++++++++----- 1 files changed, 31 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/ots/common/utils/poi/WordUtil.java b/src/main/java/com/ots/common/utils/poi/WordUtil.java index f230cd0..dcd4fac 100644 --- a/src/main/java/com/ots/common/utils/poi/WordUtil.java +++ b/src/main/java/com/ots/common/utils/poi/WordUtil.java @@ -17,6 +17,7 @@ import com.ots.project.tool.report.JAQ.condition.Table2; import com.ots.project.tool.report.JAQ.condition.Table3; import com.ots.project.tool.report.JAQ.condition.Table4; +import com.ots.project.tool.report.SAQ.chart.SAQChart; import org.apache.commons.collections.map.HashedMap; import org.apache.poi.ooxml.POIXMLDocument; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; @@ -40,6 +41,8 @@ import java.awt.image.BufferedImage; import java.io.*; import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; public class WordUtil { private static final Logger log = LoggerFactory.getLogger(WordUtil.class); @@ -177,6 +180,9 @@ if (run.toString().indexOf("%TTime%") != -1) { run.setText(textMap.get("reportGenerationDate").toString(), 0); } + if (run.toString().indexOf("%LIBSAQGS0018%") != -1) { + run.setText(textMap.get("LIBSAQGS0018").toString(), 0); + } } } } @@ -254,7 +260,7 @@ public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject, String deskFileName, int width, int height, int leftOffset, int topOffset, boolean behind, int topPosition) { - long relativeHeight = 251649024l + topPosition; + long relativeHeight = 251649024L + topPosition; String anchorXML = "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" " + "simplePos=\"0\" relativeHeight=\"" + relativeHeight + "\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">" @@ -391,7 +397,7 @@ } } private static void setTextValue(Map<String, Object> textMap, XWPFDocument document, XWPFParagraph paragraph, int runPosition, XWPFRun run) { - System.out.println("琛ㄦ牸鐨勶細" + run.toString()); + log.info("琛ㄦ牸鐨勶細{}", run.toString()); try { run.setText(changeValue(run.toString(), textMap, run, paragraph, document, runPosition), 0); } catch (Exception e) { @@ -430,7 +436,7 @@ } public static String changeValue(String runValue, Map<String, Object> textMap, XWPFRun run, XWPFParagraph paragraph, XWPFDocument document, int runPosition) throws IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException { - + log.debug("changeText:{}",runValue); Set<Map.Entry<String, Object>> textSets = textMap.entrySet(); for (Map.Entry<String, Object> textSet : textSets) { @@ -448,7 +454,11 @@ if (runValue.indexOf("<BoldText>") != -1) { return setOtherStyle(runValue, paragraph, runPosition, run); } - break; + //鏈変簺琛ㄦ牸鍙兘瀛樺湪澶氫釜瀛楀吀 闇�鏇挎崲澶氭涓嶈兘杞绘槗break + //娌℃湁鍙浛鎹㈠唴瀹圭洿鎺reak + if(getTextSize(runValue) == 0){ + break; + } } } @@ -841,8 +851,13 @@ //璁剧疆鏂囨湰 changeText(document, textMap); - //鏇存敼鏂囨湰妗� + //璁剧疆鏂囨湰妗� changeTextBox(document, textMap); + + //璁剧疆鍥捐〃 + if (Objects.equals(reportType, ReportTypeEnum.SAQ.getCode())) { + SAQChart.changeChart(document, textMap); + } //鎻掑叆琛ㄦ牸 addTableValue(document, autoTableMap); @@ -879,6 +894,7 @@ } } private static String getValByKey(String text, Map<String, Object> textMap) { + log.debug("changeTextBox:{}",text); if (!checkText(text)) { return text; } @@ -1271,4 +1287,14 @@ textMap.put("JAQTableStyle", jaqTableStyleList); } } + + public static int getTextSize(String runValue){ + Pattern pattern = Pattern.compile("%\\w+%"); + Matcher matcher = pattern.matcher(runValue); + List<String> result = new ArrayList<>(); + while(matcher.find()){ + result.add(matcher.group()); + } + return result.size(); + } } -- Gitblit v1.9.1