[测评系统]--测评系统核心代码库
linzhijie
2021-08-06 17ac23e93792f1403297dfa783c85db0c10abb20
src/main/java/com/ots/common/utils/poi/WordUtil.java
@@ -9,6 +9,7 @@
import com.ots.project.exam.domain.TReportTemplate;
import com.ots.project.exam.dto.JAQTableStyle;
import com.ots.project.exam.dto.WordParam;
import com.ots.project.tool.PdfUtil;
import com.ots.project.tool.ShellTool;
import com.ots.project.tool.exam.ExamUtil;
import com.ots.project.tool.exam.ImageUtil;
@@ -17,6 +18,7 @@
import com.ots.project.tool.report.JAQ.condition.Table2;
import com.ots.project.tool.report.JAQ.condition.Table3;
import com.ots.project.tool.report.JAQ.condition.Table4;
import com.ots.project.tool.report.SAQ.chart.SAQChart;
import org.apache.commons.collections.map.HashedMap;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -40,6 +42,8 @@
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class WordUtil {
    private static final Logger log = LoggerFactory.getLogger(WordUtil.class);
@@ -177,6 +181,9 @@
                if (run.toString().indexOf("%TTime%") != -1) {
                    run.setText(textMap.get("reportGenerationDate").toString(), 0);
                }
                if (run.toString().indexOf("%LIBSAQGS0018%") != -1) {
                    run.setText(textMap.get("LIBSAQGS0018").toString(), 0);
                }
            }
        }
    }
@@ -254,7 +261,7 @@
    public static CTAnchor getAnchorWithGraphic(CTGraphicalObject ctGraphicalObject,
                                                String deskFileName, int width, int height,
                                                int leftOffset, int topOffset, boolean behind, int topPosition) {
        long relativeHeight = 251649024l + topPosition;
        long relativeHeight = 251649024L + topPosition;
        String anchorXML =
                "<wp:anchor xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" "
                        + "simplePos=\"0\" relativeHeight=\"" + relativeHeight + "\" behindDoc=\"" + ((behind) ? 1 : 0) + "\" locked=\"0\" layoutInCell=\"1\" allowOverlap=\"1\">"
@@ -391,7 +398,7 @@
        }
    }
    private static void setTextValue(Map<String, Object> textMap, XWPFDocument document, XWPFParagraph paragraph, int runPosition, XWPFRun run) {
        System.out.println("表格的:" + run.toString());
        log.info("表格的:{}", run.toString());
        try {
            run.setText(changeValue(run.toString(), textMap, run, paragraph, document, runPosition), 0);
        } catch (Exception e) {
@@ -421,8 +428,10 @@
            }
        }
    }
    //校验文本是否需要替换
    public static boolean checkText(String text) {
        //替换文本包含%
        if (text.indexOf("%") != -1 || StringUtils.equals(text, "N/A NONE")) {
            return true;
        }
@@ -430,7 +439,7 @@
    }
    
    public static String changeValue(String runValue, Map<String, Object> textMap, XWPFRun run, XWPFParagraph paragraph, XWPFDocument document, int runPosition) throws IOException, org.apache.poi.openxml4j.exceptions.InvalidFormatException {
        log.debug("changeText:{}",runValue);
        Set<Map.Entry<String, Object>> textSets = textMap.entrySet();
        for (Map.Entry<String, Object> textSet : textSets) {
            
@@ -448,7 +457,11 @@
                if (runValue.indexOf("<BoldText>") != -1) {
                    return setOtherStyle(runValue, paragraph, runPosition, run);
                }
                break;
                //有些表格可能存在多个字典 需替换多次不能轻易break
                //没有可替换内容直接break
                if(getTextSize(runValue) == 0){
                    break;
                }
            }
        }
        
@@ -755,7 +768,12 @@
            setChageWord(fileName, out, textMap, tReportTemplate.getReportType(), templateType, autoTableMap);
            out.flush();
            changColorIfJAQ(textMap, out, reportName, tReportTemplate.getReportType());
            ShellTool.execLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/");
            //PAQ采用新的doc转pdf
            if(!ReportTypeEnum.PAQ.getCode().equals(reportType)){
                ShellTool.execLibreofficeCommand("pdf", EssConfig.getProfile() + "/" + reportName, EssConfig.getProfile() + "/");
            }else{
                PdfUtil.convertPDF(EssConfig.getProfile() + "/" + reportName);
            }
            zipFilePath = getPdfPath(reportName);
            
            deleteFileStrList.add(EssConfig.getProfile() + "/" + reportName);
@@ -841,17 +859,13 @@
        //设置文本
        changeText(document, textMap);
        //更改文本框
/*        if (Objects.equals(reportType, ReportTypeEnum.MAQV2.getCode()) || Objects.equals(reportType, ReportTypeEnum.JAQ.getCode()) ) {
            changeTextBox(document, textMap);
        }*/
        //设置文本框
        changeTextBox(document, textMap);
        //CAQ更改文本框,wzp测试.2021.4.27
/*        if (Objects.equals(reportType, ReportTypeEnum.CAQ.getCode())) {
            changeTextBox(document, textMap);
        }*/
        //设置图表
        if (Objects.equals(reportType, ReportTypeEnum.SAQ.getCode()) || Objects.equals(reportType, ReportTypeEnum.PAQ.getCode())) {
            SAQChart.changeChart(document, textMap);
        }
        //插入表格
        addTableValue(document, autoTableMap);
@@ -860,7 +874,7 @@
        changeTable(document, textMap);
        //设置图表控件
        changChar(textMap, document, reportType, templateType);
        //changChar(textMap, document, reportType, templateType);
        document.write(out);
    }
    private static void changeTextBox(XWPFDocument document, Map<String, Object> textMap) throws DocumentException {
@@ -888,6 +902,7 @@
        }
    }
    private static String getValByKey(String text, Map<String, Object> textMap) {
        log.debug("changeTextBox:{}",text);
        if (!checkText(text)) {
            return text;
        }
@@ -1280,4 +1295,21 @@
            textMap.put("JAQTableStyle", jaqTableStyleList);
        }
    }
    public static int getTextSize(String runValue){
        Pattern pattern = Pattern.compile("%\\w+%");
        Matcher matcher = pattern.matcher(runValue);
        List<String> result = new ArrayList<>();
        while(matcher.find()){
            result.add(matcher.group());
        }
        return result.size();
    }
    public static void main(String[] args) {
        if (!checkText("人才选拔的目的就是找到能够胜任工作、愿意承担工作,且与组织的需求、文化、价值观相匹配的人。PAQ报告是根据候选人对PAQ问卷中题目的作答信息而生成的,测量的是候选人在工作中偏好的行为风格。PAQ也包括了对作答真实性的测量,同时也装入了TAI专有的作假防范程序,可以有效降低候选人的伪装好倾向(全球30%的自评问卷中都出现了伪装好现象)。")) {
            System.out.println("11");
        }
    }
}