Read word template. Replace the contents of word template

/ ** 
 * Template Download 
 * 1. First, get a template, read the paragraph information template; 
 * 2. Parse paragraph information, information which a process to see what queries (queries to 380 names and then go through the process id 500 query specific data) 
 * 3. replace the template information inside the paragraph tag, e.g. $ (a), is replaced with 1; 
 * 4 template derived replaced; 
 * 
 * @param DTO 
 * @return 
 * @throws BLException 
 * @throws UnsupportedEncodingException 
 * / 
public IGOCRG01DTO downloadAttachmentAction (IGOCRG01DTO DTO) throws BLException, IOException { 
    log.info ( ">> template download operation starts ..."); 
    // 1 accepts parameters 
    IGOCRG0101Form igocrg0101Form = dto.getForm (); 
    String attrKey = igocrg0101Form.getAttachmentKey (); 
    String igocrg0101Form.getOcrgDate_Start DATE_START = (); 
    String DATE_END igocrg0101Form.getOcrgDate_End = (); 
    . // 2 reads the template key, name
    AttachmentSearchCondImpl attachmentSearchCond = new AttachmentSearchCondImpl();
    attachmentSearchCond.setAttkey(attrKey);
    List<Attachment> attachmentList = attachmentBL.searchAttachmentList(attachmentSearchCond);
    Attachment attachment = attachmentList.get(0);
    //读取指定附件
    String rootPath = "";
    rootPath += ResourceUtility.getString("UPLOAD_FILE_ROOT_PATH_OCRG");
    StringBuffer filepath = new StringBuffer(rootPath);
    filepath.append(attrKey);
    filepath.append("/");
    filepath.append(attachment.getAttname());
    //3.读取对照表 $(A):事件/服务请求管理
    HashMap<String, String> hmReplacePara = new HashMap<>();
    HashMap<String, String> hmOCRGValueToOCRGKey = new HashMap<>();
    HashMap<String, String> hmPdIdToPdName = new HashMap<>();
    ArrayList<String> alProcessName = new ArrayList<>();
    List<OcrgContrastInfo> ocrgContrastInfoList = ocrgContrastBL.searchOcrgContrast();
    for (int i = 0; i < ocrgContrastInfoList.size(); i++) {
        OcrgContrastInfo ocrgContrastInfo = ocrgContrastInfoList.get(i);
        String ocrgkey = ocrgContrastInfo.getOcrgkey();
        String ocrgvalue = ocrgContrastInfo.getOcrgvalue();
        hmOCRGValueToOCRGKey.put(ocrgvalue, ocrgkey);
        hmReplacePara.put(ocrgkey, "0");
    }
    if (filepath.toString().toLowerCase().endsWith("docx")) {
        //读取模板文件
        XWPFDocument document = new XWPFDocument(POIXMLDocument.openPackage(filepath.toString()));
        //获取文档中的段落;
        List<XWPFParagraph> xwpfParagraphList = document.getParagraphs();
        for (int i = 0; i < xwpfParagraphList.size(); i++) {
            XWPFParagraph xwpfParagraph = xwpfParagraphList.get(i);
            String paraGraphStr = xwpfParagraph.getText();
            if (StringUtils.isNotEmpty(paraGraphStr)) {
                for (Map.Entry<String, String> entry : hmOCRGValueToOCRGKey.entrySet()) {
                    String ocrgValue = entry.getValue();
                    if (paraGraphStr.contains(ocrgValue)) {
                        alProcessName.add(entry.getKey());
                    }
                }
                log.debug("文档中的段落信息: " + xwpfParagraph.getText());
            }
        }
        //读取IG380
        ArrayList<String> alPdId = new ArrayList<>();
        for (int i = 0; i < alProcessName.size(); i++) {
            //读取ig380
            IG380SearchCondImpl ig380SearchCond = new IG380SearchCondImpl();
            ig380SearchCond.setPdname(alProcessName.get(i));
            List<IG380Info> ig380InfoList = ig380BL.searchIG380Info(ig380SearchCond);
            if (ig380InfoList != null) {
                for (int j = 0; j < ig380InfoList.size(); j++) {
                    IG380Info ig380Info = ig380InfoList.get(j);
                    alPdId.add(ig380Info.getPdid());
                    hmPdIdToPdName.put(ig380Info.getPdid(), ig380Info.getPdname());
                }
            }
        }
        //查询IG500,按照流程关闭时间查询
        IG500SearchCondImpl ig500SearchCond = new IG500SearchCondImpl();
        String[] pdIdArr = new String[alPdId.size()];
        alPdId.toArray(pdIdArr);
        ig500SearchCond.setPrpdid_like_or(pdIdArr);
        ig500SearchCond.setPropentime_from(date_start + " 00:00");
        ig500SearchCond.setPropentime_to(date_end + " 23:59");
        List<IG500Info> ig500InfoList = ig500BL.searchIG500Info(ig500SearchCond);
        for (int i = 0; i < ig500InfoList.size(); i++) {
            IG500Info ig500Info = ig500InfoList.get(i);
            String pdId = ig500Info.getPrpdid();
            String pdName = hmPdIdToPdName.get(pdId);
            if (hmOCRGValueToOCRGKey.containsKey(pdName)) {
                String ocrgKey = hmOCRGValueToOCRGKey.get(pdName);
                if (hmReplacePara.containsKey(ocrgKey)) {
                    String num = hmReplacePara.get(ocrgKey);
                    hmReplacePara.put(ocrgKey, String.valueOf(Integer.parseInt(num) + 1));
                }
            }
        }
        Iterator<XWPFParagraph> itPara = document.getParagraphsIterator();
        while (itPara.hasNext()) {
            //段落信息
            = Paragraph XWPFParagraph (XWPFParagraph) itPara.next (); 
            List <XWPFRun> = paragraph.getRuns the runs (); 
            for (int I = 0; I <runs.size (); I ++) { 
                // paragraph text 
                String oneparaString = runs.get (I) .getText (runs.get (I) .getTextPosition ()); 
                for (of Map.Entry <String, String> entry: hmReplacePara.entrySet ()) { 
                    // replace paragraph of text 
                    if (oneparaString. the contains (entry.getKey ())) { 
                        log.debug ( "plaintext before replacement --docx--:" + oneparaString); 
                        oneparaString = oneparaString.replace(entry.getKey(), entry.getValue());
                    } 
                } 
                // re-set to a paragraph 
                runs.get (i) .setText (oneparaString, 0 );
                log.debug ( "plaintext ---- after replacing docx-:" + oneparaString); 
            } 
            String exceptionCondition runs.toString = (); 
            log.debug ( "Paragraph distinguish special treatment to remove the risk of generating spaces Han comma report - docx- replaced before normal text ----: "+ exceptionCondition); 
            // break API paragraphs distinguish special treatment to remove the comma space Han 
            IF (StringUtils.isNotEmpty (exceptionCondition)) { 

                exceptionCondition = exceptionCondition.replace ( ",", "") .replace ( "", ""); 
                IF (exceptionCondition.length ()> 2) 
                    exceptionCondition exceptionCondition.substring = (. 1, exceptionCondition.length () -. 1); 
                IF (exceptionCondition.contains ( "## <") && exceptionCondition.contains ( ">##")) {
                    for (of Map.Entry <String, String> entry: hmReplacePara.entrySet ()) { 
                        exceptionCondition = exceptionCondition.replace (entry.getKey (), entry.getValue ()); 
                    } 
                    runs.get (0) .setText (exceptionCondition, 0); 
                    log.info ( "paragraph distinguish special treatment to remove the spaces what comma risk report is generated - ---- docx- replacement after normal text:" + exceptionCondition); 
                    for (int I =. 1; I < runs.size (); I ++) { 
                        runs.get (I) .setText ( "", 0); 
                    } 
                } 
            } 
        } 
        the HttpServletResponse dto.getResponse Response = (); 
        response.reset ();
        the response.setContentType ( "binary / OCTET-Stream"); 
        the OutputStream Output = null; 
        the try { 
            String DocTitle = "one button to generate data reports the .docx"; 
            response.setHeader ( "the Content-Disposition", "Attachment; filename =" 
                    new new String + (docTitle.getBytes ( "GB2312"), "ISO-8859-1")); 
            Output = response.getOutputStream (); 
            // perform deriving 
            document.write (Output); 
        } the catch (Exception E) { 
            log .error ( "interface definition information - export Word abnormal"); 
            e.printStackTrace (); 
        } {the finally 
            the try { 
                // close the output stream 
                if (output!= null) {
                    output.flush (); 
                    output.close ();
                }
                IF (Output = null!) { 
                    output.flush (); 
                    output.close (); 
                } 
            } the catch (Exception E) { 
                log.error ( ">> Export Word closes the output stream Exception:" + e.getMessage ()) ; 
            } 
        } 
    } 
    log.info ( ">> template download operation is finished ..."); 
    return DTO; 
}

Guess you like

Origin blog.csdn.net/qq_34004756/article/details/91490413