2021-06-11


    /**
     * @param
     * 
    * @Description 功能描述: 根据模板中的占位符,替换对应数据后,重新生成word。 
    * @代码逻辑描述 :
    *
    * @param @param file  参数说明 
    * @return void    返回类型 
    * @throws
     */
    private XWPFDocument rebuildWord(InputStream fis, Map<String, Object> map) { 
         XWPFDocument document = null;
       
        try {
            document = new XWPFDocument(fis);
           // 替换掉表格之外的文本与统计图(仅限文本)
           changeText(document, map); 
           //替换表格
           changeTableText(document,map);            
        } catch (Exception e) {
            e.printStackTrace();
        }
    return document;    
    }

 

/**
     * 替换段落文本
     * @param <XWPFChart>
     * @param document docx解析对象
     * @param textMap 需要替换的信息集合
     * @throws Exception 
     * @throws IOException 
     *
     */
    public static <XWPFChart> void changeText(XWPFDocument document, Map<String, Object> textMap) throws IOException, Exception {
        // 获取段落集合
        // 返回包含页眉或页脚文本的段落
        List<XWPFParagraph> paragraphs = document.getParagraphs();
        // 增强型for循环语句,前面一个为声明语句,后一个为表达式
        for (XWPFParagraph paragraph : paragraphs) {
           // 判断此段落是否需要替换
            String text = paragraph.getText();// 检索文档中的所有文本
            if (checkText(text)) {
                List<XWPFRun> runs = paragraph.getRuns();
                for (XWPFRun run : runs) {
                    // 替换模板原来位置
                    Object ob = changeValue(run.toString(), textMap);
                    if (ob instanceof String) {
                        if (textMap.containsKey(run.toString())) {
                            run.setText((String) ob, 0);
                        }
                        if(run.toString().contains("${")&&run.toString().contains("}")) {
                            run.setText((String) "无", 0);
                        }
                    }
                }               
                
            }else {
                if(text.contains("{")&&text.contains("}")) {
                   String keyInParaText = text.split("\\{")[1].split("\\}")[0];
                     //表4 信用债地区分布情况
                       if("creditIndus".equals(keyInParaText)) {                           
                           insertPIC(document,paragraph,textMap,1); //柱状图
                           continue;
                       }

 
                       if("fbTrustIndustryPie".equals(keyInParaText)) {
                           insertPiePIC(document,paragraph,textMap,4);//饼图
                           continue;
                       }
                }
            }            
       }
   }


    

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值