Exemplo n.º 1
0
 /**
  * 解析这个段落
  *
  * @author Zerrion
  * @date 2013-11-16
  * @param paragraph
  * @param map
  */
 private void parseThisParagraph(XWPFParagraph paragraph, Map<String, Object> map)
     throws Exception {
   XWPFRun run;
   XWPFRun currentRun = null; // 拿到的第一个run,用来set值,可以保存格式
   String currentText = ""; // 存放当前的text
   String text;
   Boolean isfinde = false; // 判断是不是已经遇到{{
   List<Integer> runIndex = new ArrayList<Integer>(); // 存储遇到的run,把他们置空
   for (int i = 0; i < paragraph.getRuns().size(); i++) {
     run = paragraph.getRuns().get(i);
     text = run.getText(0);
     if (StringUtils.isEmpty(text)) {
       continue;
     } // 如果为空或者""这种这继续循环跳过
     if (isfinde) {
       currentText += text;
       if (currentText.indexOf("{{") == -1) {
         isfinde = false;
         runIndex.clear();
       } else {
         runIndex.add(i);
       }
       if (currentText.indexOf("}}") != -1) {
         changeValues(paragraph, currentRun, currentText, runIndex, map);
         currentText = "";
         isfinde = false;
       }
     } else if (text.indexOf("{") >= 0) { // 判断是不是开始
       currentText = text;
       isfinde = true;
       currentRun = run;
     } else {
       currentText = "";
     }
     if (currentText.indexOf("}}") != -1) {
       changeValues(paragraph, currentRun, currentText, runIndex, map);
       isfinde = false;
     }
   }
 }
Exemplo n.º 2
0
 /**
  * 根据条件改变值
  *
  * @param map
  * @author Zerrion
  * @date 2013-11-16
  */
 private void changeValues(
     XWPFParagraph paragraph,
     XWPFRun currentRun,
     String currentText,
     List<Integer> runIndex,
     Map<String, Object> map)
     throws Exception {
   Object obj = PoiPublicUtil.getRealValue(currentText, map);
   if (obj instanceof WordImageEntity) { // 如果是图片就设置为图片
     currentRun.setText("", 0);
     addAnImage((WordImageEntity) obj, currentRun);
   } else {
     currentText = obj.toString();
     currentRun.setText(currentText, 0);
   }
   for (int k = 0; k < runIndex.size(); k++) {
     paragraph.getRuns().get(runIndex.get(k)).setText("", 0);
   }
   runIndex.clear();
 }