6c
10 天以前 cfd0903dc0cfc7ffd39c2caa101f31a50441d39c
src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
@@ -1,12 +1,12 @@
package com.product.print.util;
import cn.hutool.core.collection.CollectionUtil;
import com.deepoove.poi.exception.RenderException;
import com.deepoove.poi.render.RenderContext;
import com.deepoove.poi.template.ElementTemplate;
import com.deepoove.poi.template.MetaTemplate;
import com.deepoove.poi.template.run.RunTemplate;
import com.deepoove.poi.util.TableTools;
import com.google.common.collect.Maps;
import com.product.common.lang.StringUtils;
import com.product.core.exception.BaseException;
import com.product.print.config.CmnCode;
@@ -17,7 +17,9 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -30,11 +32,10 @@
   private String replaceKey;
   private final String indexKey = "~index~";
   private final String indexKey = "~i~";
   public DynamicTableRenderPolicy(String replaceKey) {
      this.replaceKey = replaceKey;
   }
   private List<MetaTemplate> metaTemplateList;
@@ -64,6 +65,7 @@
   @Override
   public void render(XWPFTable xwpfTable, Object data) throws Exception {
        try {
      if (xwpfTable == null) {
         return;
      }
@@ -73,15 +75,14 @@
      String tableExpression = "{{" + this.replaceKey + "}}";
      //获取表格起始行和结束行
      int startRowIndex = -1;
      rows:
      for (int i = 0; i < rows.size(); i++) {
            outer: for (int i = 0; i < rows.size(); i++) {
         XWPFTableRow row = rows.get(i);
         for (int j = 0; j < row.getTableCells().size(); j++) {
            XWPFTableCell cell = row.getTableCells().get(j);
            String text = cell.getText();
            if (tableExpression.equals(text)) {
               startRowIndex = i;
               break rows;
                        break outer;
            }
         }
@@ -127,7 +128,9 @@
      }
      if (subTableData != null && subTableData.size() > 0) {
         for (int i = 0; i < subTableData.size(); i++) {
                int rowCount;
                for (int i = 0, count = 0; i < subTableData.size(); i += rowCount, count++) {
                    rowCount = 1;
            Map<String, Object> map = subTableData.get(i);
            XWPFTableRow row;
            if (i == 0) {
@@ -135,11 +138,20 @@
            } else {
               int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow);
               //创建一行在fieldRowIndex下面
               row = xwpfTable.insertNewTableRow(fieldRowIndex + i);
                        row = xwpfTable.insertNewTableRow(fieldRowIndex + count);
               PrintPoiUtil.copyTableRow(row, fieldRow);
            }
            //遍历字段每个字段创建一个单元格
            for (int j = 0; j < fieldNames.length; j++) {
                        String fieldName = fieldNames[j];
                        if (fieldName.contains(" ")) {
                            int curIndex = Integer.parseInt(fieldName.substring(fieldName.indexOf(" ") + 1));
                            if (curIndex > 1 && curIndex > rowCount) {
                                map = i + 1 == subTableData.size() ? Maps.newHashMap() : subTableData.get(i + 1);
                                rowCount = curIndex;
                            }
                            fieldName = fieldName.substring(0, fieldName.indexOf(" "));
                        }
               //当前单元格
               XWPFTableCell cell = row.getCell(j);
               //清空单元格内容
@@ -161,20 +173,20 @@
                  }
               }
               //判断是否是序号列
               if (indexKey.equals(fieldNames[j])) {
                  cell.setText(String.valueOf(i + 1));
                        if (indexKey.equals(fieldName)) {
                            if (map.isEmpty()) {
                                xwpfRun.setText("", 0);
                            } else {
                                xwpfRun.setText(String.valueOf(i + rowCount), 0);
                            }
                  continue;
               }
               Object value = map.get(fieldNames[j]);
                        Object value = map.get(fieldName);
               if (value == null) {
                  value = "";
               }
//               if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j])) {
//                  //将流程节点处理人加到意见栏中
//                  value = "[" + map.get("actual_person") + "] " + value;
//               }
               xwpfRun.setText(value.toString(), 0);
               if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j])) {
                        if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldName)) {
                  if (!StringUtils.isEmpty(map.get("sign_attachment_uuid"))) {
                     //意见框 插入签名图片到单元格右下角位置
                     xwpfParagraph = cell.addParagraph();
@@ -217,6 +229,9 @@
      }
      //删除起始行
      xwpfTable.removeRow(startRowIndex);
        } catch (Exception e) {
            e.printStackTrace();
        }
   }
   /**