月球大数据地理空间分析展示平台-【后端】-月球后台服务
1
13693261870
2024-11-17 796b44ea813a1133beae4f3a67f1c0263510c0c7
src/main/java/com/moon/server/helper/WordHelper.java
@@ -9,16 +9,10 @@
import java.util.List;
import java.util.Map;
/**
 * Word帮助类
 * @author WWW
 */
@SuppressWarnings("ALL")
public class WordHelper {
    private final static Log log = LogFactory.getLog(WordHelper.class);
    /**
     * 通过word模板生成word的主方法
     */
    public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
@@ -27,18 +21,14 @@
            inputStream = new FileInputStream(inputFile);
            outputStream = new FileOutputStream(outPutFile);
            // 获取docx解析对象
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
            // 处理所有文段数据,除了表格
            if (null != insertTextMap && insertTextMap.size() > 0) {
                handleParagraphs(xwpfDocument, insertTextMap);
            }
            // 处理表格数据
            handleTable(xwpfDocument, insertTextMap, addList);
            // 写入数据
            xwpfDocument.write(outputStream);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
@@ -56,24 +46,17 @@
        }
    }
    /**
     * 处理所有文段数据,除了表格
     */
    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> map, List<String[]> addList) {
        List<XWPFTable> tables = xwpfDocument.getTables();
        for (XWPFTable table : tables) {
@@ -94,9 +77,6 @@
        }
    }
    /**
     * 替换数据
     */
    private static void replaceData(List<XWPFTableRow> rows, Map<String, String> map) {
        for (XWPFTableRow row : rows) {
            List<XWPFTableCell> tableCells = row.getTableCells();
@@ -114,9 +94,6 @@
        }
    }
    /**
     * 插入数据
     */
    private static void insertData(XWPFTable table, List<String[]> addList) {
        for (int i = 1, c = addList.size(); i < c; i++) {
            table.createRow();
@@ -142,9 +119,6 @@
        }
    }
    /**
     * 设置单元格文本
     */
    private static void setCellText(XWPFTableCell cell, String text) {
        List<XWPFParagraph> paragraphs = cell.getParagraphs();
        for (XWPFParagraph paragraph : paragraphs) {
@@ -155,13 +129,6 @@
        }
    }
    /**
     * 有${}的值匹配出替换的数据,没有${}就返回原来的数据
     *
     * @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 + "}";
@@ -173,16 +140,10 @@
        return wordValue;
    }
    /**
     * 测试是否包含需要替换的数据
     */
    public static boolean isReplacement(String text) {
        return text.contains("$");
    }
    /**
     * 复制模板行的属性
     */
    private static void setCellText(XWPFTableCell tmpCell, XWPFTableCell cell, String text) {
        CTTc cttc2 = tmpCell.getCTTc();
        CTTcPr ctPr2 = cttc2.getTcPr();
@@ -207,10 +168,8 @@
        XWPFRun cellR = cellP.createRun();
        cellR.setText(text);
        // 复制字体信息
        copyFontInfo(cellR, tmpR);
        // 复制段落信息
        cellP.setAlignment(tmpP.getAlignment());
        cellP.setVerticalAlignment(tmpP.getVerticalAlignment());
        cellP.setBorderBetween(tmpP.getBorderBetween());
@@ -224,17 +183,11 @@
            CTPPr tmpPpr = tmpP.getCTP().getPPr();
            CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
            // 复制段落间距信息
            copySpacing(tmpPpr, cellPpr);
            // 复制段落缩进信息
            copyParagraph(tmpPpr, cellPpr);
        }
    }
    /**
     * 复制字体信息
     */
    private static void copyFontInfo(XWPFRun cellR, XWPFRun tmpR) {
        if (tmpR == null) {
            return;
@@ -274,9 +227,6 @@
        }
    }
    /**
     * 复制段落间距信息
     */
    private static void copySpacing(CTPPr tmpPpr, CTPPr cellPpr) {
        CTSpacing tmpSpacing = tmpPpr.getSpacing();
        if (tmpSpacing != null) {
@@ -310,9 +260,6 @@
        }
    }
    /**
     * 复制段落缩进信息
     */
    private static void copyParagraph(CTPPr tmpPpr, CTPPr cellPpr) {
        CTInd tmpInd = tmpPpr.getInd();
        if (tmpInd != null) {