管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-10 421ecefe1a6885727c327d0c1bb2f70d5bb91753
1
已添加1个文件
已修改1个文件
143 ■■■■■ 文件已修改
pom.xml 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/WordHelper.java 120 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
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>
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解析对象
        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()) {
                    // åˆ¤æ–­å¸¦æœ‰ ${} çš„run
                    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("$");
    }
}