From 421ecefe1a6885727c327d0c1bb2f70d5bb91753 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期五, 10 二月 2023 16:38:51 +0800
Subject: [PATCH] 1

---
 src/main/java/com/lf/server/helper/WordHelper.java |  120 ++++++++++++++++++++++++++++++++++++++++
 pom.xml                                            |   23 +++++++
 2 files changed, 143 insertions(+), 0 deletions(-)

diff --git a/pom.xml b/pom.xml
index 3c70b5e..cadd57a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -242,6 +242,29 @@
             <artifactId>fast-md5</artifactId>
             <version>2.7.1</version>
         </dependency>
+        <!--apache.poi-->
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi</artifactId>
+            <version>5.2.2</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-ooxml</artifactId>
+            <version>5.2.2</version>
+            <scope>compile</scope>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.poi</groupId>
+            <artifactId>poi-excelant</artifactId>
+            <version>5.2.2</version>
+        </dependency>
+        <dependency>
+            <groupId>org.apache.xmlbeans</groupId>
+            <artifactId>xmlbeans</artifactId>
+            <version>2.6.0</version>
+        </dependency>
     </dependencies>
 
     <build>
diff --git a/src/main/java/com/lf/server/helper/WordHelper.java b/src/main/java/com/lf/server/helper/WordHelper.java
new file mode 100644
index 0000000..1881103
--- /dev/null
+++ b/src/main/java/com/lf/server/helper/WordHelper.java
@@ -0,0 +1,120 @@
+package com.lf.server.helper;
+
+import org.apache.poi.xwpf.usermodel.*;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * Word甯姪绫�
+ * @author WWW
+ */
+public class WordHelper {
+    /**
+     * 閫氳繃word妯℃澘鐢熸垚word鐨勪富鏂规硶
+     */
+    public static void generateWord(InputStream inputStream, OutputStream outputStream, Map<String, String> insertTextMap, List<String[]> addList) throws IOException {
+        // 鑾峰彇docx瑙f瀽瀵硅薄
+        XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
+
+        // 澶勭悊鎵�鏈夋枃娈垫暟鎹紝闄や簡琛ㄦ牸
+        handleParagraphs(xwpfDocument, insertTextMap);
+
+        // 澶勭悊琛ㄦ牸鏁版嵁
+        handleTable(xwpfDocument, insertTextMap, addList);
+
+        // 鍐欏叆鏁版嵁
+        xwpfDocument.write(outputStream);
+
+        outputStream.close();
+    }
+
+    /**
+     * 澶勭悊鎵�鏈夋枃娈垫暟鎹紝闄や簡琛ㄦ牸
+     */
+    public static void handleParagraphs(XWPFDocument xwpfDocument, Map<String, String> insertTextMap) {
+        for (XWPFParagraph paragraph : xwpfDocument.getParagraphs()) {
+            String text = paragraph.getText();
+            if (isReplacement(text)) {
+                for (XWPFRun run : paragraph.getRuns()) {
+                    // 鍒ゆ柇甯︽湁 ${} 鐨剅un
+                    run.setText(matchesValue(run.text(), insertTextMap), 0);
+                }
+            }
+        }
+    }
+
+    /**
+     * 澶勭悊琛ㄦ牸鏁版嵁鏂规硶
+     */
+    public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> insertTextMap, List<String[]> addList) {
+        List<XWPFTable> tables = xwpfDocument.getTables();
+        for (XWPFTable table : tables) {
+            List<XWPFTableRow> rows = table.getRows();
+            if (rows.size() < 2) {
+                continue;
+            }
+
+            if (isReplacement(table.getText())) {
+                // 鏇挎崲鏁版嵁
+                for (XWPFTableRow row : rows) {
+                    List<XWPFTableCell> tableCells = row.getTableCells();
+                    for (XWPFTableCell tableCell : tableCells) {
+                        if (isReplacement(tableCell.getText())) {
+                            List<XWPFParagraph> paragraphs = tableCell.getParagraphs();
+                            for (XWPFParagraph paragraph : paragraphs) {
+                                List<XWPFRun> runs = paragraph.getRuns();
+                                for (XWPFRun run : runs) {
+                                    run.setText(matchesValue(tableCell.getText(), insertTextMap), 0);
+                                }
+                            }
+                        }
+                    }
+                }
+            } else {
+                // 鎻掑叆鏁版嵁
+                for (int i = 1; i < addList.size(); i++) {
+                    XWPFTableRow row = table.createRow();
+                }
+
+                List<XWPFTableRow> rowList = table.getRows();
+                for (int i = 1; i < rowList.size(); i++) {
+                    XWPFTableRow xwpfTableRow = rowList.get(i);
+                    List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
+                    for (int j = 0; j < tableCells.size(); j++) {
+                        XWPFTableCell xwpfTableCell = tableCells.get(j);
+                        xwpfTableCell.setText(addList.get(i - 1)[j]);
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 鏈�${}鐨勫�煎尮閰嶅嚭鏇挎崲鐨勬暟鎹紝娌℃湁${}灏辫繑鍥炲師鏉ョ殑鏁版嵁
+     *
+     * @param wordValue ${...} 甯�${}鐨勫彉閲�
+     * @param map       瀛樺偍闇�瑕佹浛鎹㈢殑鏁版嵁
+     * @return java.lang.String
+     */
+    public static String matchesValue(String wordValue, Map<String, String> map) {
+        for (String s : map.keySet()) {
+            String s1 = "${" + s + "}";
+            if (s1.equals(wordValue)) {
+                wordValue = map.get(s);
+            }
+        }
+
+        return wordValue;
+    }
+
+    /**
+     * 娴嬭瘯鏄惁鍖呭惈闇�瑕佹浛鎹㈢殑鏁版嵁
+     */
+    public static boolean isReplacement(String text) {
+        return text.contains("$");
+    }
+}

--
Gitblit v1.9.3