[测评系统]--测评系统核心代码库
林致杰
2022-08-15 bce3ed8cfae1da18f9b75f70bdc533d9605f49aa
src/main/java/com/ots/project/tool/report/MAQTR/chart/MAQTRChart.java
@@ -1,6 +1,7 @@
package com.ots.project.tool.report.MAQTR.chart;
import com.alibaba.fastjson.JSON;
import com.ots.common.enums.CapabilityEnum;
import com.ots.common.utils.StringUtils;
import com.ots.project.tool.report.MAQ.base.RowData;
import com.ots.project.tool.report.MAQ.base.SeriesData;
@@ -8,13 +9,13 @@
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.POIXMLDocumentPart;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.CellType;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.XWPFChart;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.*;
import org.openxmlformats.schemas.drawingml.x2006.chart.*;
import java.io.*;
@@ -33,15 +34,16 @@
    public static void main(String[] args) throws IOException {
        try {
/*            OutputStream os = new FileOutputStream("C:\\Users\\大头\\Desktop\\MAQ组别报告\\林_PAQ_IA_CN1.docx");
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("C:\\Users\\大头\\Desktop\\MAQ组别报告\\终稿【0607中文版】MAQ组别对比报告.docx"));
            OutputStream os = new FileOutputStream("C:\\Users\\大头\\Desktop\\MAQ组别报告\\林_PAQ_IA_CN2.docx");
            XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage("C:\\Users\\大头\\Desktop\\MAQ组别报告\\终稿【0607中文版】MAQ组别对比报告(1).docx"));
            String fileName = "C:\\Users\\大头\\Desktop\\MAQ组别报告\\MAQ组别导入数据模板.xlsx";
            String fileName = "C:\\Users\\大头\\Desktop\\MAQ组别报告\\MAQ组别导入数据模板 -3组(1).xlsx";
            //变更图表数据
            changeMAQTRChart(document,fileName);
            document.write(os);*/
            String fileName = "C:\\Users\\大头\\Desktop\\MAQ组别报告\\组别对比报告测试数据.xlsx";
            replaceContent(fileName);
            document.write(os);
/*            String fileName = "C:\\Users\\大头\\Desktop\\MAQ组别报告\\组别对比报告测试数据.xlsx";
            //replaceContent(fileName);
            initDataNew(fileName,1);*/
        }catch (Exception e){
            e.printStackTrace();
        }
@@ -51,43 +53,216 @@
    /**
     * 计算数据并生成模板
     */
    public static void replaceContent(String fileName){
    public static Map<String,Map<Integer,Double>> replaceContent(String fileName){
        //平均值集合
        Map<String,Map<Integer,Double>> averageMap = new TreeMap<>();
        try {
            XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream(fileName));
            Map<String,Map<Integer,List<Double>>> dataMap = new HashMap<>();
            Map<String,Map<Integer,List<Double>>> dataMap = new TreeMap<>();
            for (int i = 0; i < wb.getNumberOfSheets(); i++) {
                XSSFSheet sheet = wb.getSheetAt(i);
                String companyName = sheet.getRow(1).getCell(0).getStringCellValue();
                Map<Integer,List<Double>> map = new HashMap<>();
                //统计人数
                int person = 0;
                //行
                for (int j = 1; j < sheet.getLastRowNum(); j++) {
                for (int j = 1; j <= sheet.getLastRowNum(); j++) {
                    XSSFRow row = sheet.getRow(j);
                    if(isEmptyRow(row)){
                        break;
                    }
                    //列
                    for (int k = 1; k < row.getLastCellNum(); k++) {
                        XSSFCell cell = row.getCell(k);
/*
                        if (CellType.NUMERIC == cell.getCellType()) {
                            System.out.println(cell.getNumericCellValue());
                        } else if (CellType.STRING == cell.getCellType()) {
                            System.out.println(cell.getStringCellValue());
                        if(cell == null){
                            break;
                        }
*/
                        //table1 多个答题结果处理
                        //不是分数 忽略
                        Double cellValue = null;
                        try {
                            switch (cell.getCellType()) {
                                case STRING:
                                    cellValue = Double.valueOf(cell.getStringCellValue());
                                    break;
                                case NUMERIC:
                                    cellValue = cell.getNumericCellValue();
                                    break;
                            }
                        }catch (Exception e){
                            log.error("获取分数失败,{}"+e.getMessage());
                            e.printStackTrace();
                        }
                        //归类个性
                        if(map.get(k) != null){
                            map.get(k).add(cell.getNumericCellValue());
                            map.get(k).add(cellValue);
                        }else {
                            List<Double> list = new ArrayList<>();
                            list.add(cell.getNumericCellValue());
                            list.add(cellValue);
                            map.put(k,list);
                        }
                    }
                    person++;
                }
                //设置人数
                List<Double> list = new ArrayList<>();
                list.add(Double.valueOf(person));
                map.put(999,list);
                //设置不同团队的数据
                dataMap.put(companyName,map);
            }
            System.out.println(dataMap);
            //平均值集合
            for (Map.Entry<String, Map<Integer, List<Double>>> map : dataMap.entrySet()) {
                //33个个性集合值
                Map<Integer,List<Double>> valMap = map.getValue();
                //统计个性平均值
                Map<Integer,Double> doubleMap = new HashMap<>();
                for (Map.Entry<Integer, List<Double>> val : valMap.entrySet()) {
                    List<Double> doubleList = val.getValue();
                    //计算平均值
                    double average = doubleList.stream().mapToDouble(e -> e).average().orElse(0.00);
                    doubleMap.put(val.getKey(),average);
                }
                averageMap.put(map.getKey(),doubleMap);
            }
            //System.out.println(averageMap);
        }catch (Exception e){
            e.printStackTrace();
            log.error("计算平均值出错!");
        }
        return averageMap;
    }
    /**
     * 初始化报告数据
     * @param fileName 数据源文件名
     * @param index 模板sheet索引
     * @return
     */
    public static List<SeriesData> initDataNew(String fileName,Integer index){
        Map<String,Map<Integer,Double>> dataMap = replaceContent(fileName);
        List<SeriesData> seriesDatas = new ArrayList<>();
        if(dataMap != null){
            int oneIndex = 22;
            int twoIndex = 33;
            //1-22个性
            if(index == 0){
                //个性 [第一行]
                SeriesData title = new SeriesData();
                String name = "个性";
                title.setName(name);
                List<RowData> rowDataList = new ArrayList<>();
                for (int i = oneIndex; i > 0; i--) {
                    RowData rowData = new RowData();
                    rowData.setName(name);
                    rowData.setValueStr(CapabilityEnum.codeOf(i));
                    rowDataList.add(rowData);
                }
                title.setValue(rowDataList);
                seriesDatas.add(title);
                //动态值
                for (Map.Entry<String,Map<Integer,Double>> map : dataMap.entrySet()) {
                    SeriesData valSeriesData = new SeriesData();
                    Map<Integer,Double> valMap = map.getValue();
                    valSeriesData.setName(map.getKey()+"(n="+ valMap.get(999).intValue() +")");
                    List<RowData> valRowDataList = new ArrayList<>();
                    for (int i = oneIndex; i > 0; i--) {
                        RowData rowData = new RowData();
                        rowData.setName(map.getKey());
                        rowData.setValue(valMap.get(i));
                        valRowDataList.add(rowData);
                    }
                    valSeriesData.setValue(valRowDataList);
                    seriesDatas.add(valSeriesData);
                }
                //固定辅助列
                String auxiliary = "辅助";
                SeriesData auxiliaryTitle = new SeriesData();
                auxiliaryTitle.setName(auxiliary);
                List<RowData> auxiliaryRowDataList = new ArrayList<>();
                for (int i = 1; i <= oneIndex; i++) {
                    RowData rowData = new RowData();
                    rowData.setName(auxiliary);
                    rowData.setValue(i);
                    auxiliaryRowDataList.add(rowData);
                }
                auxiliaryTitle.setValue(auxiliaryRowDataList);
                seriesDatas.add(auxiliaryTitle);
                //坐标线
                String blank = " ";
                SeriesData blankTitle = new SeriesData();
                blankTitle.setName(blank);
                List<RowData> blankTitleRowDataList = new ArrayList<>();
                for (int i = 1; i <= oneIndex; i++) {
                    RowData rowData = new RowData();
                    rowData.setName(blank);
                    rowData.setValue(0);
                    blankTitleRowDataList.add(rowData);
                }
                blankTitle.setValue(blankTitleRowDataList);
                seriesDatas.add(blankTitle);
            }
            //23-33个性
            if(index == 1){
                //个性 [第一行]
                SeriesData title = new SeriesData();
                String name = "个性";
                title.setName(name);
                List<RowData> rowDataList = new ArrayList<>();
                for (int i = twoIndex; i > oneIndex; i--) {
                    RowData rowData = new RowData();
                    rowData.setName(name);
                    rowData.setValueStr(CapabilityEnum.codeOf(i));
                    rowDataList.add(rowData);
                }
                title.setValue(rowDataList);
                seriesDatas.add(title);
                //动态值
                for (Map.Entry<String,Map<Integer,Double>> map : dataMap.entrySet()) {
                    SeriesData valSeriesData = new SeriesData();
                    Map<Integer,Double> valMap = map.getValue();
                    valSeriesData.setName(map.getKey()+"(n="+valMap.get(999).intValue()+")");
                    List<RowData> valRowDataList = new ArrayList<>();
                    for (int i = twoIndex; i > oneIndex; i--) {
                        RowData rowData = new RowData();
                        rowData.setName(map.getKey());
                        rowData.setValue(valMap.get(i));
                        valRowDataList.add(rowData);
                    }
                    valSeriesData.setValue(valRowDataList);
                    seriesDatas.add(valSeriesData);
                }
                //固定辅助列
                String auxiliary = "辅助";
                SeriesData auxiliaryTitle = new SeriesData();
                auxiliaryTitle.setName(auxiliary);
                List<RowData> auxiliaryRowDataList = new ArrayList<>();
                for (int i = 1; i <= (twoIndex-oneIndex); i++) {
                    RowData rowData = new RowData();
                    rowData.setName(auxiliary);
                    rowData.setValue(i);
                    auxiliaryRowDataList.add(rowData);
                }
                auxiliaryTitle.setValue(auxiliaryRowDataList);
                seriesDatas.add(auxiliaryTitle);
                //坐标线
                String blank = " ";
                SeriesData blankTitle = new SeriesData();
                blankTitle.setName(blank);
                List<RowData> blankTitleRowDataList = new ArrayList<>();
                for (int i = 1; i <= (twoIndex-oneIndex); i++) {
                    RowData rowData = new RowData();
                    rowData.setName(blank);
                    rowData.setValue(0);
                    blankTitleRowDataList.add(rowData);
                }
                blankTitle.setValue(blankTitleRowDataList);
                seriesDatas.add(blankTitle);
            }
        }
        return seriesDatas;
    }
    /**
@@ -200,7 +375,9 @@
                        index = 1;
                    }
                    //获取模板数据
                    List<SeriesData> seriesDatas = initData(fileName,index);
                    List<SeriesData> seriesDatas = initDataNew(fileName,index);
                    //List<SeriesData> seriesDatas = initData(fileName,index);
                    //log.error(JSON.toJSONString(seriesDatas));
                    //数据长度
                    int size = seriesDatas.size();
                    // 查看里面的图表数据,才能知道是什么图表
@@ -242,7 +419,7 @@
                            }
                            CTScatterSer ser = scatterChart.getSerList().get(i);
                            updateScatterChart(scatterDatas, ser.getXVal(), ser.getYVal());
                            updateScatterChart(scatterDatas,ser.getTx(), ser.getXVal(), ser.getYVal());
                        }
                    }
                }
@@ -301,6 +478,29 @@
    }
    /**
     * 空行判断
     * @param row
     * @return
     */
    public static boolean isEmptyRow(XSSFRow row){
        if(row == null || row.toString().isEmpty()){
            return  true;
        }else{
            Iterator<Cell> it = row.iterator();
            boolean isEmpty = true;
            while (it.hasNext()){
                Cell cell = it.next();
                if(cell.getCellType() != CellType.BLANK){
                    isEmpty = false;
                    break;
                }
            }
            return isEmpty;
        }
    }
    /**
     * 更新图表的关联 excel
     *
     * @param seriesDatas
@@ -347,6 +547,9 @@
            //log.error("lastRowNum:{},size:{}",lastRowNum,size);
            if (lastRowNum > size) {
                for (int idx = lastRowNum; idx > size; idx--) {
                    if(sheet.getRow(idx) == null){
                        continue;
                    }
                    sheet.removeRow(sheet.getRow(idx));
                }
            }
@@ -361,9 +564,15 @@
     * @param xDataSource X坐标数据缓存
     * @param yDataSource Y坐标数据缓存
     */
    protected static void updateScatterChart(List<SeriesData> seriesDatas, CTAxDataSource xDataSource,
    protected static void updateScatterChart(List<SeriesData> seriesDatas,CTSerTx serTitle, CTAxDataSource xDataSource,
                                             CTNumDataSource yDataSource) {
        // 更新系列标题
        if(StringUtils.isNotEmpty(seriesDatas.get(0).getName())){
            serTitle.getStrRef().setF(serTitle.getStrRef().getF());
            serTitle.getStrRef().getStrCache().getPtArray(0).setV(seriesDatas.get(0).getName());
        }
        //获取xy坐标数据条数
        long xNumCnt = xDataSource.getNumRef().getNumCache().getPtCount().getVal();
        long yNumCnt = yDataSource.getNumRef().getNumCache().getPtCount().getVal();