licailing 4 лет назад
Родитель
Сommit
2e38290c70

+ 20 - 0
src/main/java/com/izouma/wenlvju/dto/ChartItem.java

@@ -0,0 +1,20 @@
+package com.izouma.wenlvju.dto;
+
+import lombok.AllArgsConstructor;
+import lombok.Builder;
+import lombok.Data;
+import lombok.NoArgsConstructor;
+
+import java.util.List;
+import java.util.Map;
+
+@Builder
+@NoArgsConstructor
+@AllArgsConstructor
+@Data
+public class ChartItem {
+    List<String>              titleArr;
+    List<String>              fldNameArr;
+    List<Map<String, String>> listItemsByType;
+    String                    type;
+}

+ 128 - 325
src/main/java/com/izouma/wenlvju/service/poi/PoiWordTable.java

@@ -1,5 +1,7 @@
 package com.izouma.wenlvju.service.poi;
 
+import cn.hutool.core.util.ObjectUtil;
+import com.izouma.wenlvju.dto.ChartItem;
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.util.Units;
@@ -8,49 +10,13 @@ import org.apache.xmlbeans.XmlCursor;
 import org.springframework.util.StringUtils;
 
 import java.io.*;
-import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.Set;
 
 public class PoiWordTable {
 
-    public static void main(String[] args) throws Exception {
-
-        final String returnurl = "D:\\youxi\\jx\\result.docx";  // 结果文件
-
-        final String templateurl = "D:\\youxi\\jx\\test.docx";  // 模板文件
-
-        InputStream is = new FileInputStream(templateurl);
-        XWPFDocument doc = new XWPFDocument(is);
-
-        // 替换word模板数据
-        replaceAll(doc);
-
-        // 保存结果文件
-        try {
-            File file = new File(returnurl);
-            if (file.exists()) {
-                file.delete();
-            }
-            FileOutputStream fos = new FileOutputStream(returnurl);
-            doc.write(fos);
-            fos.close();
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-    }
-
-
-    /**
-     * @Description: 替换段落和表格中
-     */
-    public static void replaceAll(XWPFDocument doc) throws InvalidFormatException, IOException {
-        doParagraphs(doc); // 处理段落文字数据,包括文字和表格、图片
-        doCharts(doc);  // 处理图表数据,柱状图、折线图、饼图啊之类的
-    }
-
-
     /**
      * 处理段落文字
      *
@@ -59,49 +25,41 @@ public class PoiWordTable {
      * @throws FileNotFoundException
      * @throws IOException
      */
-    public static void doParagraphs(XWPFDocument doc) throws InvalidFormatException, IOException {
+    public static void doParagraphs(XWPFDocument doc, Map<String, String> textMap, List<List<String[]>> tableList) throws InvalidFormatException, IOException {
 
-        // 文本数据
-        Map<String, String> textMap = new HashMap<>();
-        textMap.put("var", "我是被替换的文本内容");
-
-        // 图片数据
-        Map<String, String> imgMap = new HashMap<>();
-        imgMap.put("img", "D:/youxi/ddd.png");
-
-
-        /**----------------------------处理段落------------------------------------**/
         List<XWPFParagraph> paragraphList = doc.getParagraphs();
         if (paragraphList != null && paragraphList.size() > 0) {
+            int tableN = 0;
             for (XWPFParagraph paragraph : paragraphList) {
                 List<XWPFRun> runs = paragraph.getRuns();
+
                 for (XWPFRun run : runs) {
                     String text = run.getText(0);
                     if (text != null) {
 
                         // 替换文本信息
-                        String tempText = text;
-                        String key = tempText.replaceAll("\\{\\{", "").replaceAll("}}", "");
+                        String key = text.replaceAll("\\{\\{", "").replaceAll("}}", "");
                         if (!StringUtils.isEmpty(textMap.get(key))) {
                             run.setText(textMap.get(key), 0);
                         }
 
                         // 替换图片内容 参考:https://blog.csdn.net/a909301740/article/details/84984445
-                        String tempImgText = text;
-                        String imgkey = tempImgText.replaceAll("\\{\\{@", "").replaceAll("}}", "");
-                        if (!StringUtils.isEmpty(imgMap.get(imgkey))) {
-                            String imgPath = imgMap.get(imgkey);
-                            try {
-                                run.setText("", 0);
-                                run.addPicture(new FileInputStream(imgPath), Document.PICTURE_TYPE_PNG, "img.png", Units.toEMU(200), Units.toEMU(200));
-
-                            } catch (Exception e) {
-                                e.printStackTrace();
-                            }
-                        }
+//                        String imgkey = text.replaceAll("\\{\\{@", "").replaceAll("}}", "");
+//                        if (!StringUtils.isEmpty(imgMap.get(imgkey))) {
+//                            String imgPath = imgMap.get(imgkey);
+//                            try {
+//                                run.setText("", 0);
+//                                run.addPicture(new FileInputStream(imgPath), Document.PICTURE_TYPE_PNG, "img.png", Units.toEMU(200), Units.toEMU(200));
+//
+//                            } catch (Exception e) {
+//                                e.printStackTrace();
+//                            }
+//                        }
 
                         // 动态表格
-                        if (text.contains("${table1}")) {
+                        if (text.contains("${table")) {
+                            List<String[]> table = tableList.get(tableN);
+                            tableN++;
                             run.setText("", 0);
                             XmlCursor cursor = paragraph.getCTP().newCursor();
                             XWPFTable tableOne = doc.insertNewTbl(cursor);// ---这个是关键
@@ -111,19 +69,26 @@ public class PoiWordTable {
 
                             // 表格第一行,对于每个列,必须使用createCell(),而不是getCell(),因为第一行嘛,肯定是属于创建的,没有create哪里来的get呢
                             XWPFTableRow tableOneRowOne = tableOne.getRow(0);//行
-                            PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(0), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", "10%", "序号");
-                            PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", "45%", "公司名称(英文)");
-                            PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", "45%", "公司名称(中文)");
 
-                            // 表格第二行
-                            XWPFTableRow tableOneRowTwo = tableOne.createRow();//行
-                            PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(0), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", "10%", "一行一列");
-                            PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(1), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", "45%", "一行一列");
-                            PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(2), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", "45%", "一行一列");
+                            String[] title = table.get(0);
+                            int cellWidth = 100 / title.length;
+                            PoiWordTools.setWordCellSelfStyle(tableOneRowOne.getCell(0), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", cellWidth + "%", title[0]);
 
+                            for (int i = 1; i < title.length; i++) {
+                                PoiWordTools.setWordCellSelfStyle(tableOneRowOne.createCell(), "微软雅黑", "9", 0, "left", "top", "#000000", "#B4C6E7", cellWidth + "%", title[i]);
+                            }
 
-                            // ....... 可动态添加表格
+                            for (int i = 1; i < table.size(); i++) {
+                                // 表格第二行
+                                XWPFTableRow tableOneRowTwo = tableOne.createRow();//行
+                                String[] content = table.get(i);
+                                PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(0), "微软雅黑", "9", 0, "left", "top", "#000000", "#FFFFFF", cellWidth + "%", content[0]);
+                                for (int j = 1; j < content.length; j++) {
+                                    PoiWordTools.setWordCellSelfStyle(tableOneRowTwo.getCell(j), "微软雅黑", "9", 0, "left", "top", "#000000", "#FFFFFF", cellWidth + "%", content[j]);
+                                }
+                            }
                         }
+
                     }
                 }
             }
@@ -137,45 +102,45 @@ public class PoiWordTable {
      * @param doc
      * @throws FileNotFoundException
      */
-    public static void doCharts(XWPFDocument doc) throws FileNotFoundException {
+    public static void doCharts(XWPFDocument doc, List<ChartItem> items) throws FileNotFoundException {
         /**----------------------------处理图表------------------------------------**/
 
-        // 数据准备
-        List<String> titleArr = new ArrayList<String>();// 标题
-        titleArr.add("title");
-        titleArr.add("金额");
-
-        List<String> fldNameArr = new ArrayList<String>();// 字段名
-        fldNameArr.add("item1");
-        fldNameArr.add("item2");
-
-        // 数据集合
-        List<Map<String, String>> listItemsByType = new ArrayList<Map<String, String>>();
-
-        // 第一行数据
-        Map<String, String> base1 = new HashMap<String, String>();
-        base1.put("item1", "材料费用");
-        base1.put("item2", "500");
-
-        // 第二行数据
-        Map<String, String> base2 = new HashMap<String, String>();
-        base2.put("item1", "出差费用");
-        base2.put("item2", "300");
-
-        // 第三行数据
-        Map<String, String> base3 = new HashMap<String, String>();
-        base3.put("item1", "住宿费用");
-        base3.put("item2", "300");
-
-        listItemsByType.add(base1);
-        listItemsByType.add(base2);
-        listItemsByType.add(base3);
+//        // 数据准备
+//        List<String> titleArr = new ArrayList<>();// 标题
+//        titleArr.add("title");
+//        titleArr.add("金额");
+//
+//        List<String> fldNameArr = new ArrayList<>();// 字段名
+//        fldNameArr.add("item1");
+//        fldNameArr.add("item2");
+//
+//        // 数据集合
+//        List<Map<String, String>> listItemsByType = new ArrayList<>();
+//
+//        // 第一行数据
+//        Map<String, String> base1 = new HashMap<>();
+//        base1.put("item1", "材料费用");
+//        base1.put("item2", "500");
+//
+//        // 第二行数据
+//        Map<String, String> base2 = new HashMap<>();
+//        base2.put("item1", "出差费用");
+//        base2.put("item2", "300");
+//
+//        // 第三行数据
+//        Map<String, String> base3 = new HashMap<>();
+//        base3.put("item1", "住宿费用");
+//        base3.put("item2", "300");
+//
+//        listItemsByType.add(base1);
+//        listItemsByType.add(base2);
+//        listItemsByType.add(base3);
 
 
         // 获取word模板中的所有图表元素,用map存放
         // 为什么不用list保存:查看doc.getRelations()的源码可知,源码中使用了hashMap读取文档图表元素,
         // 对relations变量进行打印后发现,图表顺序和文档中的顺序不一致,也就是说relations的图表顺序不是文档中从上到下的顺序
-        Map<String, POIXMLDocumentPart> chartsMap = new HashMap<String, POIXMLDocumentPart>();
+        Map<String, POIXMLDocumentPart> chartsMap = new HashMap<>();
         //动态刷新图表
         List<POIXMLDocumentPart> relations = doc.getRelations();
         for (POIXMLDocumentPart poixmlDocumentPart : relations) {
@@ -192,238 +157,76 @@ public class PoiWordTable {
         }
 
         System.out.println("\n图表数量:" + chartsMap.size() + "\n");
-
-
-        // 第一个图表-条形图
-        POIXMLDocumentPart poixmlDocumentPart0 = chartsMap.get("/word/charts/chart1.xml");
-        PoiWordTools.replaceBarCharts(poixmlDocumentPart0, titleArr, fldNameArr, listItemsByType);
-
-        // 第二个-柱状图
-        POIXMLDocumentPart poixmlDocumentPart1 = chartsMap.get("/word/charts/chart2.xml");
-        PoiWordTools.replaceBarCharts(poixmlDocumentPart1, titleArr, fldNameArr, listItemsByType);
-
-        // 第三个图表-多列柱状图
-        doCharts3(chartsMap);
-
-        // 第四个图表-折线图
-        doCharts4(chartsMap);
-
-        // 第五个图表-饼图
-        POIXMLDocumentPart poixmlDocumentPart4 = chartsMap.get("/word/charts/chart5.xml");
-        PoiWordTools.replacePieCharts(poixmlDocumentPart4, titleArr, fldNameArr, listItemsByType);
-
-
-        doCharts6(chartsMap);
+        Set<String> key = chartsMap.keySet();
+        for (int i = 1; i <= key.size(); i++) {
+            POIXMLDocumentPart poixmlDocumentPart = chartsMap.get("/word/charts/chart" + i + ".xml");
+            System.out.println("/word/charts/chart" + i + ".xml");
+            if (i > items.size()) {
+                return;
+            }
+            ChartItem chartItem = items.get(i - 1);
+            if (ObjectUtil.isNull(chartItem)) {
+                return;
+            }
+            if ("bar".equals(chartItem.getType())) {
+                PoiWordTools.replaceBarCharts(poixmlDocumentPart, chartItem);
+            } else if ("line".equals(chartItem.getType())) {
+                PoiWordTools.replaceLineCharts(poixmlDocumentPart, chartItem);
+            } else if ("pie".equals(chartItem.getType())) {
+                PoiWordTools.replacePieCharts(poixmlDocumentPart, chartItem);
+            }
+        }
+//        doCharts6(chartsMap);
     }
 
 
-    public static void doCharts3(Map<String, POIXMLDocumentPart> chartsMap) {
-        // 数据准备
-        List<String> titleArr = new ArrayList<String>();// 标题
-        titleArr.add("姓名");
-        titleArr.add("欠款");
-        titleArr.add("存款");
-
-        List<String> fldNameArr = new ArrayList<String>();// 字段名
-        fldNameArr.add("item1");
-        fldNameArr.add("item2");
-        fldNameArr.add("item3");
-
-        // 数据集合
-        List<Map<String, String>> listItemsByType = new ArrayList<Map<String, String>>();
-
-        // 第一行数据
-        Map<String, String> base1 = new HashMap<String, String>();
-        base1.put("item1", "老张");
-        base1.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base1.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第二行数据
-        Map<String, String> base2 = new HashMap<String, String>();
-        base2.put("item1", "老李");
-        base2.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base2.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第三行数据
-        Map<String, String> base3 = new HashMap<String, String>();
-        base3.put("item1", "老刘");
-        base3.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base3.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-
-        listItemsByType.add(base1);
-        listItemsByType.add(base2);
-        listItemsByType.add(base3);
+    public static void doCharts3(Map<String, POIXMLDocumentPart> chartsMap, ChartItem chartItem) {
+//        // 数据准备
+//        List<String> titleArr = new ArrayList<>();// 标题
+//        titleArr.add("姓名");
+//        titleArr.add("欠款");
+//        titleArr.add("存款");
+//
+//        List<String> fldNameArr = new ArrayList<>();// 字段名
+//        fldNameArr.add("item1");
+//        fldNameArr.add("item2");
+//        fldNameArr.add("item3");
+//
+//        // 数据集合
+//        List<Map<String, String>> listItemsByType = new ArrayList<>();
+//
+//        // 第一行数据
+//        Map<String, String> base1 = new HashMap<>();
+//        base1.put("item1", "老张");
+//        base1.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
+//        base1.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
+//
+//        // 第二行数据
+//        Map<String, String> base2 = new HashMap<>();
+//        base2.put("item1", "老李");
+//        base2.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
+//        base2.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
+//
+//        // 第三行数据
+//        Map<String, String> base3 = new HashMap<String, String>();
+//        base3.put("item1", "老刘");
+//        base3.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
+//        base3.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
+//
+//
+//        listItemsByType.add(base1);
+//        listItemsByType.add(base2);
+//        listItemsByType.add(base3);
 
         POIXMLDocumentPart poixmlDocumentPart2 = chartsMap.get("/word/charts/chart3.xml");
-        PoiWordTools.replaceBarCharts(poixmlDocumentPart2, titleArr, fldNameArr, listItemsByType);
-    }
-
-
-    public static void doCharts4(Map<String, POIXMLDocumentPart> chartsMap) {
-        // 数据准备
-        List<String> titleArr = new ArrayList<String>();// 标题
-        titleArr.add("title");
-        titleArr.add("占基金资产净值比例22222(%)");
-        titleArr.add("额外的(%)");
-        titleArr.add("额外的(%)");
-
-        List<String> fldNameArr = new ArrayList<String>();// 字段名
-        fldNameArr.add("item1");
-        fldNameArr.add("item2");
-        fldNameArr.add("item3");
-        fldNameArr.add("item4");
-
-        // 数据集合
-        List<Map<String, String>> listItemsByType = new ArrayList<Map<String, String>>();
-
-        // 第一行数据
-        Map<String, String> base1 = new HashMap<String, String>();
-        base1.put("item1", "材料费用");
-        base1.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base1.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base1.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第二行数据
-        Map<String, String> base2 = new HashMap<String, String>();
-        base2.put("item1", "出差费用");
-        base2.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base2.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base2.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第三行数据
-        Map<String, String> base3 = new HashMap<String, String>();
-        base3.put("item1", "住宿费用");
-        base3.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base3.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base3.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-
-        listItemsByType.add(base1);
-        listItemsByType.add(base2);
-        listItemsByType.add(base3);
-
-        POIXMLDocumentPart poixmlDocumentPart2 = chartsMap.get("/word/charts/chart4.xml");
-        PoiWordTools.replaceLineCharts(poixmlDocumentPart2, titleArr, fldNameArr, listItemsByType);
+        PoiWordTools.replaceBarCharts(poixmlDocumentPart2, chartItem);
     }
 
 
     /**
      * 对应文档中的第6个图表(预处理—分公司情况)
      */
-    public static void doCharts6(Map<String, POIXMLDocumentPart> chartsMap) {
-        // 数据准备
-        List<String> titleArr = new ArrayList<String>();// 标题
-        titleArr.add("title");
-        titleArr.add("投诉受理量(次)");
-        titleArr.add("预处理拦截工单量(次)");
-        titleArr.add("拦截率");
-
-        List<String> fldNameArr = new ArrayList<String>();// 字段名
-        fldNameArr.add("item1");
-        fldNameArr.add("item2");
-        fldNameArr.add("item3");
-        fldNameArr.add("item4");
-
-        // 数据集合
-        List<Map<String, String>> listItemsByType = new ArrayList<Map<String, String>>();
-
-        // 第一行数据
-        Map<String, String> base1 = new HashMap<String, String>();
-        base1.put("item1", "通辽");
-        base1.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base1.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base1.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第二行数据
-        Map<String, String> base2 = new HashMap<String, String>();
-        base2.put("item1", "呼和浩特");
-        base2.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base2.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base2.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第三行数据
-        Map<String, String> base3 = new HashMap<String, String>();
-        base3.put("item1", "锡林郭勒");
-        base3.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base3.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base3.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第四行数据
-        Map<String, String> base4 = new HashMap<String, String>();
-        base4.put("item1", "阿拉善");
-        base4.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base4.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base4.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第五行数据
-        Map<String, String> base5 = new HashMap<String, String>();
-        base5.put("item1", "巴彦淖尔");
-        base5.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base5.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base5.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第六行数据
-        Map<String, String> base6 = new HashMap<String, String>();
-        base6.put("item1", "兴安");
-        base6.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base6.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base6.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第七行数据
-        Map<String, String> base7 = new HashMap<String, String>();
-        base7.put("item1", "乌兰察布");
-        base7.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base7.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base7.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第八行数据
-        Map<String, String> base8 = new HashMap<String, String>();
-        base8.put("item1", "乌海");
-        base8.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base8.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base8.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第九行数据
-        Map<String, String> base9 = new HashMap<String, String>();
-        base9.put("item1", "赤峰");
-        base9.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base9.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base9.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第十行数据
-        Map<String, String> base10 = new HashMap<String, String>();
-        base10.put("item1", "包头");
-        base10.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base10.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base10.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第十一行数据
-        Map<String, String> base11 = new HashMap<String, String>();
-        base11.put("item1", "呼伦贝尔");
-        base11.put("item2", (int) (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base11.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base11.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        // 第十二行数据
-        Map<String, String> base12 = new HashMap<String, String>();
-        base12.put("item1", "鄂尔多斯");
-        base12.put("item2", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base12.put("item3", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-        base12.put("item4", (int) (1 + Math.random() * (100 - 1 + 1)) + "");
-
-        listItemsByType.add(base1);
-        listItemsByType.add(base2);
-        listItemsByType.add(base3);
-        listItemsByType.add(base4);
-        listItemsByType.add(base5);
-        listItemsByType.add(base6);
-        listItemsByType.add(base7);
-        listItemsByType.add(base8);
-        listItemsByType.add(base9);
-        listItemsByType.add(base10);
-        listItemsByType.add(base11);
-        listItemsByType.add(base12);
+    public static void doCharts6(Map<String, POIXMLDocumentPart> chartsMap, List<String> titleArr, List<String> fldNameArr, List<Map<String, String>> listItemsByType) {
 
         // 下标0的图表-折线图
         POIXMLDocumentPart poixmlDocumentPart5 = chartsMap.get("/word/charts/chart6.xml");

+ 30 - 30
src/main/java/com/izouma/wenlvju/service/poi/PoiWordTools.java

@@ -5,9 +5,11 @@ import java.io.InputStream;
 import java.io.OutputStream;
 import java.math.BigDecimal;
 import java.math.BigInteger;
+import java.math.RoundingMode;
 import java.util.List;
 import java.util.Map;
 
+import com.izouma.wenlvju.dto.ChartItem;
 import org.apache.poi.ooxml.POIXMLDocumentPart;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.CellRangeAddress;
@@ -46,8 +48,7 @@ public class PoiWordTools {
     /**
      * 调用替换柱状图数据
      */
-    public static void replaceBarCharts(POIXMLDocumentPart poixmlDocumentPart,
-                                        List<String> titleArr, List<String> fldNameArr, List<Map<String, String>> listItemsByType) {
+    public static void replaceBarCharts(POIXMLDocumentPart poixmlDocumentPart, ChartItem item) {
         XWPFChart chart = (XWPFChart) poixmlDocumentPart;
         chart.getCTChart();
 
@@ -59,17 +60,16 @@ public class PoiWordTools {
         List<CTBarSer> BarSerList = barChart.getSerList();  // 获取柱状图单位
 
         //刷新内置excel数据
-        PoiWordTools.refreshExcel(chart, listItemsByType, fldNameArr, titleArr);
+        PoiWordTools.refreshExcel(chart, item.getListItemsByType(), item.getFldNameArr(), item.getTitleArr());
         //刷新页面显示数据
-        refreshBarStrGraphContent(barChart, BarSerList, listItemsByType, fldNameArr, 1);
+        refreshBarStrGraphContent(barChart, BarSerList, item.getListItemsByType(), item.getFldNameArr(), 1);
     }
 
 
     /**
      * 调用替换折线图数据
      */
-    public static void replaceLineCharts(POIXMLDocumentPart poixmlDocumentPart,
-                                         List<String> titleArr, List<String> fldNameArr, List<Map<String, String>> listItemsByType) {
+    public static void replaceLineCharts(POIXMLDocumentPart poixmlDocumentPart, ChartItem item) {
         XWPFChart chart = (XWPFChart) poixmlDocumentPart;
         chart.getCTChart();
 
@@ -81,9 +81,9 @@ public class PoiWordTools {
         List<CTLineSer> lineSerList = lineChart.getSerList();   // 获取折线图单位
 
         //刷新内置excel数据
-        PoiWordTools.refreshExcel(chart, listItemsByType, fldNameArr, titleArr);
+        PoiWordTools.refreshExcel(chart, item.getListItemsByType(), item.getFldNameArr(), item.getTitleArr());
         //刷新页面显示数据
-        PoiWordTools.refreshLineStrGraphContent(lineChart, lineSerList, listItemsByType, fldNameArr, 1);
+        PoiWordTools.refreshLineStrGraphContent(lineChart, lineSerList, item.getListItemsByType(), item.getFldNameArr(), 1);
 
     }
 
@@ -91,8 +91,7 @@ public class PoiWordTools {
     /**
      * 调用替换饼图数据
      */
-    public static void replacePieCharts(POIXMLDocumentPart poixmlDocumentPart,
-                                        List<String> titleArr, List<String> fldNameArr, List<Map<String, String>> listItemsByType) {
+    public static void replacePieCharts(POIXMLDocumentPart poixmlDocumentPart, ChartItem item) {
         XWPFChart chart = (XWPFChart) poixmlDocumentPart;
         chart.getCTChart();
 
@@ -104,9 +103,9 @@ public class PoiWordTools {
         List<CTPieSer> pieSerList = pieChart.getSerList();  // 获取饼图单位
 
         //刷新内置excel数据
-        PoiWordTools.refreshExcel(chart, listItemsByType, fldNameArr, titleArr);
+        PoiWordTools.refreshExcel(chart, item.getListItemsByType(), item.getFldNameArr(), item.getTitleArr());
         //刷新页面显示数据
-        PoiWordTools.refreshPieStrGraphContent(pieChart, pieSerList, listItemsByType, fldNameArr, 1);
+        PoiWordTools.refreshPieStrGraphContent(pieChart, pieSerList, item.getListItemsByType(), item.getFldNameArr(), 1);
 
     }
 
@@ -170,8 +169,8 @@ public class PoiWordTools {
             // strData.set
             CTStrData strData = cat.getStrRef().getStrCache();
             CTNumData numData = val.getNumRef().getNumCache();
-            strData.setPtArray((CTStrVal[]) null); // unset old axis text
-            numData.setPtArray((CTNumVal[]) null); // unset old values
+            strData.setPtArray(null); // unset old axis text
+            numData.setPtArray(null); // unset old values
 
             // set model
             long idx = 0;
@@ -223,8 +222,8 @@ public class PoiWordTools {
      * @param position
      * @return
      */
-    public  static boolean refreshBarStrGraphContent(Object typeChart,
-                                                     List<?> serList, List<Map<String, String>> dataList, List<String> fldNameArr, int position) {
+    public static boolean refreshBarStrGraphContent(Object typeChart,
+                                                    List<?> serList, List<Map<String, String>> dataList, List<String> fldNameArr, int position) {
         boolean result = true;
         //更新数据区域
         for (int i = 0; i < serList.size(); i++) {
@@ -246,11 +245,11 @@ public class PoiWordTools {
 
             // set model
             long idx = 0;
-            for (int j = 0; j < dataList.size(); j++) {
+            for (Map<String, String> stringStringMap : dataList) {
                 //判断获取的值是否为空
                 String value = "0";
-                if (new BigDecimal(dataList.get(j).get(fldNameArr.get(i + position))) != null) {
-                    value = new BigDecimal(dataList.get(j).get(fldNameArr.get(i + position))).toString();
+                if (new BigDecimal(stringStringMap.get(fldNameArr.get(i + position))) != null) {
+                    value = new BigDecimal(stringStringMap.get(fldNameArr.get(i + position))).toString();
                 }
                 if (!"0".equals(value)) {
                     CTNumVal numVal = numData.addNewPt();//序列值
@@ -259,7 +258,7 @@ public class PoiWordTools {
                 }
                 CTStrVal sVal = strData.addNewPt();//序列名称
                 sVal.setIdx(idx);
-                sVal.setV(dataList.get(j).get(fldNameArr.get(0)));
+                sVal.setV(stringStringMap.get(fldNameArr.get(0)));
                 idx++;
             }
             numData.getPtCount().setVal(idx);
@@ -281,7 +280,6 @@ public class PoiWordTools {
     }
 
 
-
     /**
      * 刷新饼图数据方法
      *
@@ -319,9 +317,8 @@ public class PoiWordTools {
             for (int j = 0; j < dataList.size(); j++) {
                 //判断获取的值是否为空
                 String value = "0";
-                if (new BigDecimal(dataList.get(j).get(fldNameArr.get(i + position))) != null) {
-                    value = new BigDecimal(dataList.get(j).get(fldNameArr.get(i + position))).toString();
-                }
+//                new BigDecimal(dataList.get(j).get(fldNameArr.get(i + position)));
+                value = new BigDecimal(dataList.get(j).get(fldNameArr.get(i + position))).toString();
                 if (!"0".equals(value)) {
                     CTNumVal numVal = numData.addNewPt();//序列值
                     numVal.setIdx(idx);
@@ -381,7 +378,9 @@ public class PoiWordTools {
                 if (sheet.getRow(i + 1) == null) {
                     if (j == 0) {
                         try {
-                            sheet.createRow(i + 1).createCell(j).setCellValue(baseFormMap.get(fldNameArr.get(j)) == null ? "" : baseFormMap.get(fldNameArr.get(j)));
+                            sheet.createRow(i + 1)
+                                    .createCell(j)
+                                    .setCellValue(baseFormMap.get(fldNameArr.get(j)) == null ? "" : baseFormMap.get(fldNameArr.get(j)));
                         } catch (Exception e) {
                             if (baseFormMap.get(fldNameArr.get(j)) == null) {
                                 sheet.createRow(i + 1).createCell(j).setCellValue("");
@@ -453,7 +452,7 @@ public class PoiWordTools {
             //poi对字体大小设置特殊,不支持小数,但对原word字体大小做了乘2处理
             BigDecimal fontSizeBD = new BigDecimal(fontSize);
             fontSizeBD = bd2.multiply(fontSizeBD);
-            fontSizeBD = fontSizeBD.setScale(0, BigDecimal.ROUND_HALF_UP);//这里取整
+            fontSizeBD = fontSizeBD.setScale(0, RoundingMode.HALF_UP);//这里取整
             bFontSize = new BigInteger(fontSizeBD.toString());// 字体大小
         }
 
@@ -485,7 +484,8 @@ public class PoiWordTools {
         //《《《《====tcPr结束====
 
         //====p开始====》》》》
-        CTP p = tc.getPList().get(0);//获取单元格里的<w:p w:rsidR="00C36068" w:rsidRPr="00B705A0" w:rsidRDefault="00C36068" w:rsidP="00C36068">
+        CTP p = tc.getPList()
+                .get(0);//获取单元格里的<w:p w:rsidR="00C36068" w:rsidRPr="00B705A0" w:rsidRDefault="00C36068" w:rsidP="00C36068">
 
         //---ppr开始--->>>
         CTPPr ppr = p.getPPr();//获取<w:p>里的<w:pPr>
@@ -538,7 +538,7 @@ public class PoiWordTools {
 
         //---r开始--->>>
         List<CTR> rlist = p.getRList(); //获取<w:p>里的<w:r w:rsidRPr="00B705A0">
-        CTR r = null;
+        CTR r;
         if (rlist != null && rlist.size() > 0) {//获取第一个<w:r>
             r = rlist.get(0);
         } else {//没有<w:r>,创建
@@ -590,7 +590,7 @@ public class PoiWordTools {
         //-<-
         //<<--rpr结束--
         List<CTText> tlist = r.getTList();
-        CTText t = null;
+        CTText t;
         if (tlist != null && tlist.size() > 0) {//获取第一个<w:r>
             t = tlist.get(0);
         } else {//没有<w:r>,创建
@@ -605,7 +605,7 @@ public class PoiWordTools {
      * 获取内置表格数据,拿到第一行第一列格子数据
      * 有时候模板设计太复杂,对于图表不能精准定位,可以通过设置图表表格数据的第一行第一列格子数据来区分,这个数据不影响图表显示,所以用来区分每个图表
      */
-    public static String getZeroData(POIXMLDocumentPart poixmlDocumentPart){
+    public static String getZeroData(POIXMLDocumentPart poixmlDocumentPart) {
         String text = "";
         try {
             XWPFChart chart = (XWPFChart) poixmlDocumentPart;

+ 184 - 5
src/main/java/com/izouma/wenlvju/service/regulation/ReportService.java

@@ -8,6 +8,7 @@ import com.izouma.wenlvju.domain.Organization;
 import com.izouma.wenlvju.domain.Record;
 import com.izouma.wenlvju.domain.RecordSpecialty;
 import com.izouma.wenlvju.domain.regulation.*;
+import com.izouma.wenlvju.dto.ChartItem;
 import com.izouma.wenlvju.dto.PageQuery;
 import com.izouma.wenlvju.dto.ReportDTO;
 import com.izouma.wenlvju.enums.ReportType;
@@ -21,6 +22,7 @@ import com.izouma.wenlvju.repo.regulation.ComplainRepo;
 import com.izouma.wenlvju.repo.regulation.RecordExpertAuditRepo;
 import com.izouma.wenlvju.repo.regulation.ReportRepo;
 import com.izouma.wenlvju.repo.regulation.ReportStatisticRepo;
+import com.izouma.wenlvju.service.poi.PoiWordTable;
 import com.izouma.wenlvju.service.storage.StorageService;
 import com.izouma.wenlvju.utils.ChartUtils;
 import com.izouma.wenlvju.utils.JpaUtils;
@@ -32,6 +34,7 @@ import lombok.AllArgsConstructor;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.io.IOUtils;
 import org.apache.commons.lang3.RandomStringUtils;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.xwpf.usermodel.XWPFChart;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.springframework.data.domain.Page;
@@ -66,6 +69,7 @@ public class ReportService {
 
     /**
      * 获取时间
+     *
      * @param report
      * @return
      */
@@ -312,11 +316,11 @@ public class ReportService {
             }
         }
         List<NumOfReport> reports = new ArrayList<>();
-        reports.add(new NumOfReport("music", sum01));
-        reports.add(new NumOfReport("dance", sum02));
-        reports.add(new NumOfReport("fineArt", sum03));
-        reports.add(new NumOfReport("operaDrama", sum04));
-        reports.add(new NumOfReport("folkArt", sum05));
+        reports.add(new NumOfReport("音乐", sum01));
+        reports.add(new NumOfReport("舞蹈", sum02));
+        reports.add(new NumOfReport("美术", sum03));
+        reports.add(new NumOfReport("戏曲戏剧", sum04));
+        reports.add(new NumOfReport("曲艺", sum05));
         return reports;
     }
 
@@ -335,6 +339,7 @@ public class ReportService {
 
     /**
      * 生成word
+     *
      * @param report
      * @throws IOException
      * @throws TemplateException
@@ -443,6 +448,180 @@ public class ReportService {
         reportStatisticRepo.saveAll(statistics);
     }
 
+    public void poiWord(Report report) throws IOException, InvalidFormatException {
+        // 开始结束时间
+        Map<String, Object> date = this.getDate(report);
+        if (ObjectUtil.isNull(date)) {
+            throw new BusinessException("该文档已生成");
+        }
+
+        LocalDateTime start = LocalDateTime.parse(String.valueOf(date.get("start")));
+        LocalDateTime end = LocalDateTime.parse(String.valueOf(date.get("end")));
+        //备案
+        List<Record> records = recordRepo.findAllByExaminationStartTimeBetween(start.toLocalDate(), end.toLocalDate());
+        //投诉
+        List<Complain> complains = complainRepo.findAllByFinishIsTrueAndComplainAtBetween(start, end);
+        //备案专业
+        List<Long> recordIds = records.stream().map(Record::getId).collect(Collectors.toList());
+        List<RecordSpecialty> recordSpecialties = recordSpecialtyRepo.findAllByRecordIdIn(recordIds);
+        //备案检查
+        List<RecordExpertAudit> recordAudits = recordExpertAuditRepo.findAllByRecordIdIn(recordIds);
+
+        //标题
+        String time = String.valueOf(date.get("time"));
+        final String returnurl = "/Users/qiufangchao/Desktop/result.docx";  // 结果文件
+        final String templateurl = "/Users/qiufangchao/Desktop/poiTemp.docx";  // 模板文件
+
+        InputStream is = new FileInputStream(templateurl);
+        XWPFDocument doc = new XWPFDocument(is);
+
+        Map<String, String> textMap = new HashMap<>();
+        List<List<String[]>> tableList = new ArrayList<>();
+        List<ChartItem> items = new ArrayList<>();
+
+        textMap.put("Title", time + "南京市社会艺术水平考级监管报告");
+
+        //总统计
+        List<NumOfReport> total = this.getTotal(records, complains, recordAudits);
+        List<String[]> tableTotal = new ArrayList<>();
+        tableTotal.add(new String[]{"备案条数", "报考人数", "投诉数量", "检查数量"});
+        Map<String, Integer> totalMap = total.stream()
+                .collect(Collectors.toMap(NumOfReport::getName, NumOfReport::getNum));
+        tableTotal.add(new String[]{String.valueOf(totalMap.get("record")), String.valueOf(totalMap.get("totalExamQuantity")),
+                String.valueOf(totalMap.get("totalComplain")), String.valueOf(totalMap.get("totalAudit"))});
+        tableList.add(tableTotal);
+
+        //各专业统计
+        List<NumOfReport> specialty = this.getSpecialty(recordSpecialties);
+        List<String[]> tableSpecialty = new ArrayList<>();
+//        tableSpecialty.add((String[]) total.stream().map(NumOfReport::getName).toArray());
+//        tableSpecialty.add((String[]) total.stream().map(NumOfReport::getNum).toArray());
+        Map<String, Integer> specialtyMap = specialty.stream()
+                .collect(Collectors.toMap(NumOfReport::getName, NumOfReport::getNum));
+        tableSpecialty.add(new String[]{"音乐", "舞蹈", "美术", "戏曲戏剧", "曲艺"});
+        tableSpecialty.add(new String[]{String.valueOf(specialtyMap.get("音乐")), String.valueOf(specialtyMap.get("舞蹈")),
+                String.valueOf(specialtyMap.get("美术")), String.valueOf(specialtyMap.get("戏曲戏剧")), String.valueOf(specialtyMap.get("曲艺"))});
+        tableList.add(tableSpecialty);
+
+        //画图
+        List<String> titleArr = new ArrayList<>();// 标题
+        titleArr.add("title");
+        titleArr.add("各专业报考场数");
+
+        List<String> fldNameArr = new ArrayList<>();// 字段名
+        fldNameArr.add("key");
+        fldNameArr.add("value");
+
+        List<Map<String, String>> listItemsByType = new ArrayList<>();
+        specialty.forEach(sp -> {
+            Map<String, String> con = new HashMap<>();
+            con.put("key", sp.getName());
+            con.put("value", String.valueOf(sp.getNum()));
+            listItemsByType.add(con);
+        });
+
+        items.add(ChartItem.builder()
+                .fldNameArr(fldNameArr)
+                .type("pie")
+                .titleArr(titleArr)
+                .listItemsByType(listItemsByType)
+                .build());
+
+        //考级机构/承办单位统计
+        List<ReportStatistic> reportDTOS = this.generate(records, complains);
+        Map<UnitType, List<ReportStatistic>> unitTypeListMap = reportDTOS.stream()
+                .collect(Collectors.groupingBy(ReportStatistic::getUnitType));
+        //考级机构
+        List<ReportStatistic> reportsGo = unitTypeListMap.get(UnitType.GRADING_ORGANIZATION);
+        String[] goTile = {"机构名称", "考级数量", "报考人数", "投诉条数", "投诉内容"};
+        List<String[]> tableGo = new ArrayList<>();
+        tableGo.add(goTile);
+        reportsGo.forEach(dto -> tableGo.add(new String[]{dto.getName(), String.valueOf(dto.getExamCenterQuantity()), String.valueOf(dto.getExamQuantity()),
+                String.valueOf(dto.getComplain()), dto.getComplainContent()}));
+        tableList.add(tableGo);
+
+        //承办单位
+        List<ReportStatistic> reportsOr = unitTypeListMap.get(UnitType.ORGANIZATION);
+        List<String[]> tableOr = new ArrayList<>();
+        tableOr.add(goTile);
+        reportsOr.forEach(dto -> tableOr.add(new String[]{dto.getName(), String.valueOf(dto.getExamCenterQuantity()), String.valueOf(dto.getExamQuantity()),
+                String.valueOf(dto.getComplain()), dto.getComplainContent()}));
+        tableList.add(tableOr);
+
+        //投诉饼图
+        //画图
+        List<String> titleCom = new ArrayList<>();// 标题
+        titleCom.add("title");
+        titleCom.add("考级机构投诉统计");
+
+        List<Map<String, String>> comItemsByType = new ArrayList<>();
+        reportsGo.forEach(sp -> {
+            Map<String, String> con = new HashMap<>();
+            con.put("key", sp.getName());
+            con.put("value", String.valueOf(sp.getComplain()));
+            comItemsByType.add(con);
+        });
+
+        items.add(ChartItem.builder()
+                .fldNameArr(fldNameArr)
+                .type("pie")
+                .titleArr(titleCom)
+                .listItemsByType(comItemsByType)
+                .build());
+
+        //备案柱状图
+        List<String> titleRecord = new ArrayList<>();// 标题
+        titleRecord.add("title");
+        titleRecord.add("承办单位报考人数统计");
+
+        List<Map<String, String>> recordItemsByType = new ArrayList<>();
+        reportsOr.forEach(sp -> {
+            Map<String, String> con = new HashMap<>();
+            con.put("key", sp.getName());
+            con.put("value", String.valueOf(sp.getExamQuantity()));
+            recordItemsByType.add(con);
+        });
+
+        items.add(ChartItem.builder()
+                .fldNameArr(fldNameArr)
+                .type("bar")
+                .titleArr(titleRecord)
+                .listItemsByType(recordItemsByType)
+                .build());
+
+        // 生成文档
+        PoiWordTable.doParagraphs(doc, textMap, tableList);
+        PoiWordTable.doCharts(doc, items);
+
+        try {
+            File file = new File(returnurl);
+            if (file.exists()) {
+                file.delete();
+            }
+            FileOutputStream fos = new FileOutputStream(returnurl);
+            doc.write(fos);
+            fos.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+
+//        InputStream is = IOUtils.toInputStream(writer.toString(), "UTF-8");
+//
+//        String url = storageService.uploadFromInputStream(is, "word" + "/" + new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss").format(new Date())
+//                + RandomStringUtils.randomAlphabetic(8) + ".docx");
+//
+//        report.setSpecialty(specialty);
+//        report.setTotal(total);
+//        report.setFile(url);
+//        report.setTime(time);
+//        report.setTitle("南京市社会艺术水平考级监管报告");
+//        Long id = reportRepo.save(report).getId();
+//        List<ReportStatistic> statistics = reportsOr.stream()
+//                .peek(statistic -> statistic.setReportId(id))
+//                .collect(Collectors.toList());
+//        reportStatisticRepo.saveAll(statistics);
+    }
+
     /*
     画图
      */

+ 6 - 0
src/main/java/com/izouma/wenlvju/web/regulation/ReportController.java

@@ -11,6 +11,7 @@ import com.izouma.wenlvju.utils.excel.ExcelUtils;
 
 import freemarker.template.TemplateException;
 import lombok.AllArgsConstructor;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.springframework.data.domain.Page;
 import org.springframework.security.access.prepost.PreAuthorize;
 import org.springframework.web.bind.annotation.*;
@@ -65,5 +66,10 @@ public class ReportController extends BaseController {
     public void word(@RequestBody Report report) throws TemplateException, IOException {
         reportService.word(report);
     }
+
+    @PostMapping("/poiWord")
+    public void poiWord(@RequestBody Report report) throws IOException, InvalidFormatException {
+        reportService.poiWord(report);
+    }
 }
 

+ 10 - 10
src/main/vue/src/widgets/ExamWidegt.vue

@@ -57,16 +57,16 @@ export default {
             //     });
             // });
             res.forEach(item => {
-                let name = '音乐';
-                if (item.name == 'dance') {
-                    name = '舞蹈';
-                } else if (item.name == 'fineArt') {
-                    name = '美术';
-                } else if (item.name == 'operaDrama') {
-                    name = '戏曲戏剧';
-                } else if (item.name == 'folkArt') {
-                    name = '曲艺';
-                }
+                // let name = '音乐';
+                // if (item.name == 'dance') {
+                //     name = '舞蹈';
+                // } else if (item.name == 'fineArt') {
+                //     name = '美术';
+                // } else if (item.name == 'operaDrama') {
+                //     name = '戏曲戏剧';
+                // } else if (item.name == 'folkArt') {
+                //     name = '曲艺';
+                // }
                 data.push({
                     name: name,
                     value: item.num

+ 86 - 5
src/test/java/com/izouma/wenlvju/service/regulation/ReportServiceTest.java

@@ -3,8 +3,11 @@ package com.izouma.wenlvju.service.regulation;
 
 import com.izouma.wenlvju.ApplicationTests;
 import com.izouma.wenlvju.domain.regulation.Report;
+import com.izouma.wenlvju.dto.ChartItem;
 import com.izouma.wenlvju.enums.ReportType;
+import com.izouma.wenlvju.service.poi.PoiWordTable;
 import com.izouma.wenlvju.utils.ChartUtils;
+import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
 import org.apache.poi.xwpf.usermodel.XWPFChart;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.junit.Test;
@@ -67,15 +70,93 @@ public class ReportServiceTest extends ApplicationTests {
 
     @Test
     public void test3() throws IOException {
-        Map<String,Integer> map = new HashMap();
-        map.put("音乐",100);
-        map.put("舞蹈",89);
-        map.put("美术",109);
-        map.put("戏曲戏剧",120);
+        Map<String, Integer> map = new HashMap();
+        map.put("音乐", 100);
+        map.put("舞蹈", 89);
+        map.put("美术", 109);
+        map.put("戏曲戏剧", 120);
 
         XWPFDocument x = reportService.chart(map, "/templates/chart/PieTemplate.docx", "专业");
         try (FileOutputStream fos = new FileOutputStream("/Users/qiufangchao/Desktop/test.docx")) {
             x.write(fos);
         }
     }
+
+    @Test
+    public void test4() throws IOException, InvalidFormatException {
+        final String returnurl = "/Users/qiufangchao/Desktop/result.docx";  // 结果文件
+
+        final String templateurl = "/Users/qiufangchao/Desktop/poiTemp.docx";  // 模板文件
+
+        InputStream is = new FileInputStream(templateurl);
+        XWPFDocument doc = new XWPFDocument(is);
+
+        Map<String, String> textMap = new HashMap<>();
+        textMap.put("Title", "2021年南京市社会艺术水平考级监管报告");
+
+        List<String[]> table = new ArrayList<>();
+        table.add(new String[]{"备案条数", "报考人数", "投诉数量", "检查数量"});
+        table.add(new String[]{"2000", "4988", "286", "1987"});
+        List<String[]> table1 = new ArrayList<>();
+        table1.add(new String[]{"备案条数1", "报考人数1", "投诉数量1", "检查数量1"});
+        table1.add(new String[]{"1765", "88", "1286", "1827"});
+        List<List<String[]>> tableList = new ArrayList<>();
+        tableList.add(table);
+        tableList.add(table1);
+        PoiWordTable.doParagraphs(doc, textMap, tableList);
+
+        List<ChartItem> items = new ArrayList<>();
+        List<String> titleArr = new ArrayList<>();// 标题
+        titleArr.add("title");
+        titleArr.add("各专业报考场数");
+
+        List<String> fldNameArr = new ArrayList<>();// 字段名
+        fldNameArr.add("item1");
+        fldNameArr.add("item2");
+
+        List<Map<String, String>> listItemsByType = new ArrayList<>();
+        // 第一行数据
+        Map<String, String> base1 = new HashMap<>();
+        base1.put("item1", "音乐");
+        base1.put("item2", "500");
+        Map<String, String> base2 = new HashMap<>();
+        base2.put("item1", "舞蹈");
+        base2.put("item2", "240");
+        Map<String, String> base3 = new HashMap<>();
+        base3.put("item1", "美术");
+        base3.put("item2", "470");
+        listItemsByType.add(base1);
+        listItemsByType.add(base2);
+        listItemsByType.add(base3);
+
+        items.add(ChartItem.builder()
+                .fldNameArr(fldNameArr)
+                .listItemsByType(listItemsByType)
+                .titleArr(titleArr)
+                .type("pie")
+                .build());
+        PoiWordTable.doCharts(doc, items);
+        // 保存结果文件
+        try {
+            File file = new File(returnurl);
+            if (file.exists()) {
+                file.delete();
+            }
+            FileOutputStream fos = new FileOutputStream(returnurl);
+            doc.write(fos);
+            fos.close();
+        } catch (Exception e) {
+            e.printStackTrace();
+        }
+    }
+
+    @Test
+    public void test6() throws IOException, InvalidFormatException {
+        Report report = Report.builder()
+                .year(2021)
+                .type(ReportType.QUARTERLY)
+                .quarterly(4)
+                .build();
+        reportService.poiWord(report);
+    }
 }