管道基础大数据平台系统开发-【后端】-Server
1
13693261870
2023-02-10 014639637a80b0549cc0aee00b9a9670fb57827c
1
已修改3个文件
145 ■■■■■ 文件已修改
pom.xml 15 ●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/helper/WordHelper.java 107 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/main/java/com/lf/server/service/sys/ReportService.java 23 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
pom.xml
@@ -245,20 +245,19 @@
        <!--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>
            <version>3.16</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-excelant</artifactId>
            <version>5.2.2</version>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-examples</artifactId>
            <version>3.16</version>
        </dependency>
        <dependency>
            <groupId>org.apache.xmlbeans</groupId>
src/main/java/com/lf/server/helper/WordHelper.java
@@ -1,10 +1,10 @@
package com.lf.server.helper;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.poi.xwpf.usermodel.*;
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 +13,34 @@
 * @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) {
        try {
            FileInputStream inputStream = new FileInputStream(inputFile);
            FileOutputStream outputStream = new FileOutputStream(outPutFile);
        // 处理所有文段数据,除了表格
        handleParagraphs(xwpfDocument, insertTextMap);
            // 获取docx解析对象
            XWPFDocument xwpfDocument = new XWPFDocument(inputStream);
        // 处理表格数据
        handleTable(xwpfDocument, insertTextMap, addList);
            // 处理所有文段数据,除了表格
            if (null != insertTextMap && insertTextMap.size() > 0) {
                handleParagraphs(xwpfDocument, insertTextMap);
            }
        // 写入数据
        xwpfDocument.write(outputStream);
            // 处理表格数据
            handleTable(xwpfDocument, insertTextMap, addList);
        outputStream.close();
            // 写入数据
            xwpfDocument.write(outputStream);
            outputStream.close();
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }
    /**
@@ -50,7 +61,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,36 +70,52 @@
            }
            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; 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]);
            }
        }
    }
src/main/java/com/lf/server/service/sys/ReportService.java
@@ -3,10 +3,12 @@
import com.lf.server.entity.ctrl.CountEntity;
import com.lf.server.entity.sys.ReportEntity;
import com.lf.server.helper.StringHelper;
import com.lf.server.helper.WordHelper;
import com.lf.server.mapper.sys.ReportMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
/**
@@ -86,4 +88,25 @@
    public List<CountEntity> countOperates() {
        return reportMapper.countOperates();
    }
    /**
     * 创建 用户流量统计 Word
     */
    public void createCountSizesWord() {
        List<CountEntity> list = countOperates();
        if (null == list || list.isEmpty()) {
            return;
        }
        String inputFile = "D:\\LF\\用户流量统计.docx";
        String outPutFile = "D:\\LF\\用户流量统计_new.docx";
        ArrayList<String[]> addList = new ArrayList<>();
        for (CountEntity ce : list) {
            String[] strs = new String[]{ce.getM1(), ce.getM2(), ce.getCount().toString()};
            addList.add(strs);
        }
        WordHelper.generateWord(inputFile, outPutFile, null, addList);
    }
}