管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-10 9b6fc58ae37b2745310e6942af1a4dae51a82c56
src/main/java/com/lf/server/helper/WordHelper.java
@@ -1,10 +1,11 @@
package com.lf.server.helper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.*;
import java.util.List;
import java.util.Map;
@@ -13,23 +14,46 @@
 * @author WWW
 */
public class WordHelper {
    private final static Log log = LogFactory.getLog(WordHelper.class);
    /**
     * 通过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);
    public static void generateWord(String inputFile, String outPutFile, Map<String, String> insertTextMap, List<String[]> addList) {
        FileInputStream inputStream = null;
        FileOutputStream outputStream = null;
        // 处理所有文段数据,除了表格
        handleParagraphs(xwpfDocument, insertTextMap);
        try {
            inputStream = new FileInputStream(inputFile);
            outputStream = new FileOutputStream(outPutFile);
        // 处理表格数据
        handleTable(xwpfDocument, insertTextMap, addList);
            // 获取docx解析对象
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
        // 写入数据
        xwpfDocument.write(outputStream);
            // 处理所有文段数据,除了表格
            if (null != insertTextMap && insertTextMap.size() > 0) {
                handleParagraphs(xwpfDocument, insertTextMap);
            }
        outputStream.close();
            // 处理表格数据
            handleTable(xwpfDocument, insertTextMap, addList);
            // 写入数据
            xwpfDocument.write(outputStream);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }
    /**
@@ -50,7 +74,7 @@
    /**
     * 处理表格数据方法
     */
    public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> insertTextMap, List<String[]> addList) {
    public static void handleTable(XWPFDocument xwpfDocument, Map<String, String> map, List<String[]> addList) {
        List<XWPFTable> tables = xwpfDocument.getTables();
        for (XWPFTable table : tables) {
            List<XWPFTableRow> rows = table.getRows();
@@ -59,35 +83,59 @@
            }
            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);
                                }
                            }
                if (null == map || map.isEmpty()) {
                    continue;
                }
                replaceData(rows, map);
            } else {
                insertData(table, addList);
            }
        }
    }
    /**
     * 替换数据
     */
    private static void replaceData(List<XWPFTableRow> rows, Map<String, String> map) {
        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(), map), 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]);
                    }
    /**
     * 插入数据
     */
    private static void insertData(XWPFTable table, List<String[]> addList) {
        for (int i = 1, c = addList.size(); i < c; i++) {
            XWPFTableRow row = table.createRow();
        }
        List<XWPFTableCell> oldCells = table.getRow(1).getTableCells();
        List<XWPFTableRow> rowList = table.getRows();
        for (int i = 0, c = addList.size(); i < c; i++) {
            XWPFTableRow xwpfTableRow = rowList.get(i + 1);
            List<XWPFTableCell> tableCells = xwpfTableRow.getTableCells();
            for (int j = 0; j < tableCells.size(); j++) {
                XWPFTableCell oldCell = oldCells.get(j);
                XWPFTableCell newCell = tableCells.get(j);
                if (0 == i) {
                    newCell.setText(addList.get(i)[j]);
                } else {
                    setCellText(oldCell, newCell, addList.get(i)[j]);
                }
            }
        }
@@ -117,4 +165,150 @@
    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();
        CTTc cttc = cell.getCTTc();
        CTTcPr ctPr = cttc.addNewTcPr();
        if (ctPr2.getTcW() != null) {
            ctPr.addNewTcW().setW(ctPr2.getTcW().getW());
        }
        if (ctPr2.getVAlign() != null) {
            ctPr.addNewVAlign().setVal(ctPr2.getVAlign().getVal());
        }
        if (ctPr2.getTcBorders() != null) {
            ctPr.setTcBorders(ctPr2.getTcBorders());
        }
        XWPFParagraph tmpP = tmpCell.getParagraphs().get(0);
        XWPFParagraph cellP = cell.getParagraphs().get(0);
        XWPFRun tmpR = null;
        if (tmpP.getRuns() != null && tmpP.getRuns().size() > 0) {
            tmpR = tmpP.getRuns().get(0);
        }
        XWPFRun cellR = cellP.createRun();
        cellR.setText(text);
        // 复制字体信息
        if (tmpR != null) {
            if (!cellR.isBold()) {
                cellR.setBold(tmpR.isBold());
            }
            cellR.setItalic(tmpR.isItalic());
            cellR.setUnderline(tmpR.getUnderline());
            cellR.setColor(tmpR.getColor());
            cellR.setTextPosition(tmpR.getTextPosition());
            if (tmpR.getFontSize() != -1) {
                cellR.setFontSize(tmpR.getFontSize());
            }
            if (tmpR.getFontFamily() != null) {
                cellR.setFontFamily(tmpR.getFontFamily());
            }
            if (tmpR.getCTR() != null) {
                if (tmpR.getCTR().isSetRPr()) {
                    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();
                        cellFonts.setAscii(tmpFonts.getAscii());
                        cellFonts.setAsciiTheme(tmpFonts.getAsciiTheme());
                        cellFonts.setCs(tmpFonts.getCs());
                        cellFonts.setCstheme(tmpFonts.getCstheme());
                        cellFonts.setEastAsia(tmpFonts.getEastAsia());
                        cellFonts.setEastAsiaTheme(tmpFonts.getEastAsiaTheme());
                        cellFonts.setHAnsi(tmpFonts.getHAnsi());
                        cellFonts.setHAnsiTheme(tmpFonts.getHAnsiTheme());
                    }
                }
            }
        }
        // 复制段落信息
        cellP.setAlignment(tmpP.getAlignment());
        cellP.setVerticalAlignment(tmpP.getVerticalAlignment());
        cellP.setBorderBetween(tmpP.getBorderBetween());
        cellP.setBorderBottom(tmpP.getBorderBottom());
        cellP.setBorderLeft(tmpP.getBorderLeft());
        cellP.setBorderRight(tmpP.getBorderRight());
        cellP.setBorderTop(tmpP.getBorderTop());
        cellP.setPageBreak(tmpP.isPageBreak());
        if (tmpP.getCTP() != null) {
            if (tmpP.getCTP().getPPr() != null) {
                CTPPr tmpPpr = tmpP.getCTP().getPPr();
                CTPPr cellPpr = cellP.getCTP().getPPr() != null ? cellP.getCTP().getPPr() : cellP.getCTP().addNewPPr();
                // 复制段落间距信息
                CTSpacing tmpSpacing = tmpPpr.getSpacing();
                if (tmpSpacing != null) {
                    CTSpacing cellSpacing = cellPpr.getSpacing() != null ? cellPpr
                            .getSpacing() : cellPpr.addNewSpacing();
                    if (tmpSpacing.getAfter() != null) {
                        cellSpacing.setAfter(tmpSpacing.getAfter());
                    }
                    if (tmpSpacing.getAfterAutospacing() != null) {
                        cellSpacing.setAfterAutospacing(tmpSpacing
                                .getAfterAutospacing());
                    }
                    if (tmpSpacing.getAfterLines() != null) {
                        cellSpacing.setAfterLines(tmpSpacing.getAfterLines());
                    }
                    if (tmpSpacing.getBefore() != null) {
                        cellSpacing.setBefore(tmpSpacing.getBefore());
                    }
                    if (tmpSpacing.getBeforeAutospacing() != null) {
                        cellSpacing.setBeforeAutospacing(tmpSpacing
                                .getBeforeAutospacing());
                    }
                    if (tmpSpacing.getBeforeLines() != null) {
                        cellSpacing.setBeforeLines(tmpSpacing.getBeforeLines());
                    }
                    if (tmpSpacing.getLine() != null) {
                        cellSpacing.setLine(tmpSpacing.getLine());
                    }
                    if (tmpSpacing.getLineRule() != null) {
                        cellSpacing.setLineRule(tmpSpacing.getLineRule());
                    }
                }
                // 复制段落缩进信息
                CTInd tmpInd = tmpPpr.getInd();
                if (tmpInd != null) {
                    CTInd cellInd = cellPpr.getInd() != null ? cellPpr.getInd()
                            : cellPpr.addNewInd();
                    if (tmpInd.getFirstLine() != null) {
                        cellInd.setFirstLine(tmpInd.getFirstLine());
                    }
                    if (tmpInd.getFirstLineChars() != null) {
                        cellInd.setFirstLineChars(tmpInd.getFirstLineChars());
                    }
                    if (tmpInd.getHanging() != null) {
                        cellInd.setHanging(tmpInd.getHanging());
                    }
                    if (tmpInd.getHangingChars() != null) {
                        cellInd.setHangingChars(tmpInd.getHangingChars());
                    }
                    if (tmpInd.getLeft() != null) {
                        cellInd.setLeft(tmpInd.getLeft());
                    }
                    if (tmpInd.getLeftChars() != null) {
                        cellInd.setLeftChars(tmpInd.getLeftChars());
                    }
                    if (tmpInd.getRight() != null) {
                        cellInd.setRight(tmpInd.getRight());
                    }
                    if (tmpInd.getRightChars() != null) {
                        cellInd.setRightChars(tmpInd.getRightChars());
                    }
                }
            }
        }
    }
}