From b9b490e0ec226389c6e2502fecf918f7537d8c57 Mon Sep 17 00:00:00 2001
From: 13693261870 <252740454@qq.com>
Date: 星期四, 21 三月 2024 10:57:52 +0800
Subject: [PATCH] 添加Word Excel测试方法

---
 src/main/java/com/lf/server/helper/WordHelper.java |  205 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 files changed, 189 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/lf/server/helper/WordHelper.java b/src/main/java/com/lf/server/helper/WordHelper.java
index 5704e07..c977318 100644
--- a/src/main/java/com/lf/server/helper/WordHelper.java
+++ b/src/main/java/com/lf/server/helper/WordHelper.java
@@ -8,18 +8,22 @@
 import java.io.*;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 /**
  * Word甯姪绫�
+ *
  * @author WWW
+ * @date 2024-03-21
  */
 public class WordHelper {
     private final static Log log = LogFactory.getLog(WordHelper.class);
 
     /**
      * 閫氳繃word妯℃澘鐢熸垚word鐨勪富鏂规硶
+     * https://blog.csdn.net/qq_46112324/article/details/128156675
      */
-    public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) {
+    public static void generateWord(String inputFile, String outPutFile, Map<String, String> textMap, List<String[]> tableList) {
         FileInputStream inputStream = null;
         FileOutputStream outputStream = null;
 
@@ -28,18 +32,23 @@
             outputStream = new FileOutputStream(outPutFile);
 
             // 鑾峰彇docx瑙f瀽瀵硅薄
-            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
+            XWPFDocument document = new XWPFDocument(inputStream);
 
             // 澶勭悊鎵�鏈夋枃娈垫暟鎹紝闄や簡琛ㄦ牸
-            if (null != insertTextMap && insertTextMap.size() > 0) {
-                handleParagraphs(xwpfDocument, insertTextMap);
+            if (null != textMap && textMap.size() > 0) {
+                handleParagraphs(document, textMap);
+                //changeText(document, textMap);
             }
 
-            // 澶勭悊琛ㄦ牸鏁版嵁
-            handleTable(xwpfDocument, insertTextMap, addList);
+            // 瑙f瀽鏇挎崲琛ㄦ牸瀵硅薄
+            if (null != tableList && tableList.size() > 0) {
+                handleTable(document, textMap, tableList);
+                //changeTable(document, textMap, tableList);
+            }
 
             // 鍐欏叆鏁版嵁
-            xwpfDocument.write(outputStream);
+            document.write(outputStream);
+            document.close();
         } catch (Exception ex) {
             log.error(ex.getMessage(), ex);
         } finally {
@@ -55,6 +64,170 @@
             }
         }
     }
+
+    /**
+     * 鏇挎崲琛ㄦ牸瀵硅薄鏂规硶
+     *
+     * @param document  docx瑙f瀽瀵硅薄
+     * @param textMap   闇�瑕佹浛鎹㈢殑淇℃伅闆嗗悎
+     * @param tableList 闇�瑕佹彃鍏ョ殑琛ㄦ牸淇℃伅闆嗗悎
+     */
+    public static void changeTable(XWPFDocument document, Map<String, String> textMap, List<String[]> tableList) {
+        // 鑾峰彇琛ㄦ牸瀵硅薄闆嗗悎
+        List<XWPFTable> tables = document.getTables();
+        for (int i = 0; i < tables.size(); i++) {
+            XWPFTable table = tables.get(i);
+            if (table.getRows().size() > 1) {
+                // 鍒ゆ柇琛ㄦ牸鏄渶瑕佹浛鎹㈣繕鏄渶瑕佹彃鍏ワ紝鍒ゆ柇閫昏緫鏈�$涓烘浛鎹紝琛ㄦ牸鏃�$涓烘彃鍏�
+                if (!checkText(table.getText())) {
+                    insertTable(table, tableList);
+                }
+            }
+        }
+    }
+
+    /**
+     * 鏇挎崲娈佃惤鏂囨湰
+     *
+     * @param document docx瑙f瀽瀵硅薄
+     * @param textMap  闇�瑕佹浛鎹㈢殑淇℃伅闆嗗悎
+     */
+    public static void changeText(XWPFDocument document, Map<String, String> textMap) {
+        // 鑾峰彇娈佃惤闆嗗悎
+        List<XWPFTable> tables = document.getTables();
+        for (XWPFTable table : tables) {
+            if (checkText(table.getText())) {
+                List<XWPFTableRow> rows = table.getRows();
+                for (XWPFTableRow row : rows) {
+                    List<XWPFTableCell> tableCells = row.getTableCells();
+                    for (XWPFTableCell cell : tableCells) {
+                        if (checkText(cell.getText())) {
+                            List<XWPFParagraph> paragraphs = cell.getParagraphs();
+                            for (XWPFParagraph paragraph : paragraphs) {
+                                // 鍒ゆ柇姝ゆ钀芥椂鍊欓渶瑕佽繘琛屾浛鎹�
+                                String text = paragraph.getText();
+                                if (checkText(text)) {
+                                    List<XWPFRun> runs = paragraph.getRuns();
+                                    for (XWPFRun run : runs) {
+                                        // 鏇挎崲妯℃澘鍘熸潵浣嶇疆
+                                        if (checkText(run.toString())) {
+                                            run.setText(changeValue(paragraph.getText(), textMap), 0);
+                                        } else {
+                                            run.setText("", 0);
+                                        }
+                                    }
+                                }
+                            }
+                        }
+                    }
+                }
+            }
+        }
+    }
+
+    /**
+     * 鍒ゆ柇鏂囨湰涓椂鍊欏寘鍚�$
+     *
+     * @param text 鏂囨湰
+     * @return 鍖呭惈杩斿洖true, 涓嶅寘鍚繑鍥瀎alse
+     */
+    public static boolean checkText(String text) {
+        boolean check = false;
+        if (text.contains("$")) {
+            check = true;
+        }
+        return check;
+    }
+
+    /**
+     * 鍖归厤浼犲叆淇℃伅闆嗗悎涓庢ā鏉�
+     *
+     * @param value   妯℃澘闇�瑕佹浛鎹㈢殑鍖哄煙
+     * @param textMap 浼犲叆淇℃伅闆嗗悎
+     * @return 妯℃澘闇�瑕佹浛鎹㈠尯鍩熶俊鎭泦鍚堝搴斿��
+     */
+    public static String changeValue(String value, Map<String, String> textMap) {
+        Set<Map.Entry<String, String>> textSets = textMap.entrySet();
+        for (Map.Entry<String, String> textSet : textSets) {
+            // 鍖归厤妯℃澘涓庢浛鎹㈠�� 鏍煎紡${key}
+            String key = "${" + textSet.getKey() + "}";
+            if (key.contains(value)) {
+                value = textSet.getValue();
+            }
+        }
+        return value;
+    }
+
+    /**
+     * 涓鸿〃鏍兼彃鍏ユ暟鎹紝琛屾暟涓嶅娣诲姞鏂拌
+     *
+     * @param table     闇�瑕佹彃鍏ユ暟鎹殑琛ㄦ牸
+     * @param tableList 鎻掑叆鏁版嵁闆嗗悎
+     */
+    public static void insertTable(XWPFTable table, List<String[]> tableList) {
+        // 璁板綍闇�瑕佹彃鍏ョ殑绗竴琛�
+        int start = -1;
+        for (int i = 0; i < table.getRows().size(); i++) {
+            for (int j = 0; j < table.getRows().get(i).getTableCells().size(); j++) {
+                if ("".equals(table.getRows().get(i).getTableCells().get(j).getText())) {
+                    start = i;
+                    break;
+                }
+            }
+        }
+        // 鍒涘缓琛�,鏍规嵁闇�瑕佹彃鍏ョ殑鏁版嵁娣诲姞鏂拌
+        if (start != -1) {
+            for (int i = 1; i < tableList.size(); i++) {
+                insertRow(table, start, start + i);
+            }
+        }
+
+        // 閬嶅巻琛ㄦ牸鎻掑叆鏁版嵁
+        List<XWPFTableRow> rows = table.getRows();
+        for (int i = start; i < rows.size(); i++) {
+            List<XWPFTableCell> cells = rows.get(i).getTableCells();
+            for (int j = 0; j < table.getRow(start).getTableCells().size(); j++) {
+                XWPFTableCell cell = cells.get(j);
+                cell.setText(tableList.get(0)[j]);
+            }
+            tableList.remove(0);
+        }
+    }
+
+    /**
+     * 涓鸿〃鏍兼彃鍏ユ暟鎹紝琛屾暟涓嶅娣诲姞鏂拌
+     *
+     * @param table        闇�瑕佹彃鍏ユ暟鎹殑琛ㄦ牸
+     * @param copyRowIndex 澶嶅埗琛屽彿
+     * @param newRowIndex  鏂拌鍙�
+     */
+    public static void insertRow(XWPFTable table, int copyRowIndex, int newRowIndex) {
+        // 鍦ㄨ〃鏍间腑鎸囧畾鐨勪綅缃柊澧炰竴琛�
+        XWPFTableRow targetRow = table.insertNewTableRow(newRowIndex);
+
+        // 鑾峰彇闇�瑕佸鍒惰瀵硅薄
+        XWPFTableRow copyRow = table.getRow(copyRowIndex);
+
+        // 澶嶅埗琛屽璞�
+        targetRow.getCtRow().setTrPr(copyRow.getCtRow().getTrPr());
+        //鎴栬闇�瑕佸鍒剁殑琛岀殑鍒�
+        List<XWPFTableCell> copyCells = copyRow.getTableCells();
+        // 澶嶅埗鍒楀璞�
+        XWPFTableCell targetCell = null;
+        for (XWPFTableCell copyCell : copyCells) {
+            targetCell = targetRow.addNewTableCell();
+            targetCell.getCTTc().setTcPr(copyCell.getCTTc().getTcPr());
+            if (copyCell.getParagraphs() != null && copyCell.getParagraphs().size() > 0) {
+                targetCell.getParagraphs().get(0).getCTP().setPPr(copyCell.getParagraphs().get(0).getCTP().getPPr());
+                if (copyCell.getParagraphs().get(0).getRuns() != null
+                        && copyCell.getParagraphs().get(0).getRuns().size() > 0) {
+                    XWPFRun cellR = targetCell.getParagraphs().get(0).createRun();
+                    cellR.setBold(copyCell.getParagraphs().get(0).getRuns().get(0).isBold());
+                }
+            }
+        }
+    }
+
 
     /**
      * 澶勭悊鎵�鏈夋枃娈垫暟鎹紝闄や簡琛ㄦ牸
@@ -165,8 +338,11 @@
     public static String matchesValue(String wordValue, Map<String, String> map) {
         for (String s : map.keySet()) {
             String s1 = "${" + s + "}";
-            if (s1.equals(wordValue)) {
+            /*if (s1.equals(wordValue)) {
                 wordValue = map.get(s);
+            }*/
+            if (wordValue.contains(s1)) {
+                wordValue = wordValue.replace(s1, map.get(s));
             }
         }
 
@@ -177,7 +353,7 @@
      * 娴嬭瘯鏄惁鍖呭惈闇�瑕佹浛鎹㈢殑鏁版嵁
      */
     public static boolean isReplacement(String text) {
-        return text.contains("$");
+        return text.contains("${");
     }
 
     /**
@@ -220,7 +396,7 @@
         cellP.setBorderTop(tmpP.getBorderTop());
         cellP.setPageBreak(tmpP.isPageBreak());
 
-        if (tmpP.getCTP() != null&&tmpP.getCTP().getPPr() != null) {
+        if (tmpP.getCTP() != null && tmpP.getCTP().getPPr() != null) {
             CTPPr tmpPpr = tmpP.getCTP().getPPr();
             CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
 
@@ -257,10 +433,8 @@
                 CTRPr tmpRpr = tmpR.getCTR().getRPr();
                 if (tmpRpr.isSetRFonts()) {
                     CTFonts tmpFonts = tmpRpr.getRFonts();
-                    CTRPr cellRpr = cellR.getCTR().isSetRPr() ? cellR
-                            .getCTR().getRPr() : cellR.getCTR().addNewRPr();
-                    CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr
-                            .getRFonts() : cellRpr.addNewRFonts();
+                    CTRPr cellRpr = cellR.getCTR().isSetRPr() ? cellR.getCTR().getRPr() : cellR.getCTR().addNewRPr();
+                    CTFonts cellFonts = cellRpr.isSetRFonts() ? cellRpr.getRFonts() : cellRpr.addNewRFonts();
                     cellFonts.setAscii(tmpFonts.getAscii());
                     cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme());
                     cellFonts.setCs(tmpFonts.getCs());
@@ -285,8 +459,7 @@
                 cellSpacing.setAfter(tmpSpacing.getAfter());
             }
             if (tmpSpacing.getAfterAutospacing() != null) {
-                cellSpacing.setAfterAutospacing(tmpSpacing
-                        .getAfterAutospacing());
+                cellSpacing.setAfterAutospacing(tmpSpacing.getAfterAutospacing());
             }
             if (tmpSpacing.getAfterLines() != null) {
                 cellSpacing.setAfterLines(tmpSpacing.getAfterLines());

--
Gitblit v1.9.3