From cfd0903dc0cfc7ffd39c2caa101f31a50441d39c Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期五, 31 十月 2025 17:29:59 +0800
Subject: [PATCH] 已送审保养打印; 列表批量打印
---
src/main/java/com/product/print/util/DynamicTableRenderPolicy.java | 299 +++++++++++++++++++++++++++++++++--------------------------
1 files changed, 166 insertions(+), 133 deletions(-)
diff --git a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
index daa0044..0b1d9c1 100644
--- a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
+++ b/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,15 +32,13 @@
private String replaceKey;
- private final String indexKey = "~index~";
+ private final String indexKey = "~i~";
- public DynamicTableRenderPolicy(String replaceKey) {
+ public DynamicTableRenderPolicy(String replaceKey) {
this.replaceKey = replaceKey;
-
}
private List<MetaTemplate> metaTemplateList;
-
@Override
@@ -65,140 +65,173 @@
@Override
public void render(XWPFTable xwpfTable, Object data) throws Exception {
- if (xwpfTable == null) {
- return;
- }
- List<Map<String, Object>> subTableData = (List<Map<String, Object>>) data;
- List<XWPFTableRow> rows = xwpfTable.getRows();
- //璇诲彇rows涓殑鍐呭
- String tableExpression = "{{" + this.replaceKey + "}}";
- //鑾峰彇琛ㄦ牸璧峰琛屽拰缁撴潫琛�
- int startRowIndex = -1;
- rows:
- 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;
- }
+ try {
+ if (xwpfTable == null) {
+ return;
+ }
+ List<Map<String, Object>> subTableData = (List<Map<String, Object>>) data;
+ List<XWPFTableRow> rows = xwpfTable.getRows();
+ //璇诲彇rows涓殑鍐呭
+ String tableExpression = "{{" + this.replaceKey + "}}";
+ //鑾峰彇琛ㄦ牸璧峰琛屽拰缁撴潫琛�
+ int startRowIndex = -1;
+ 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 outer;
+ }
- }
- }
- if (startRowIndex == -1) {
- throw new BaseException(CmnCode.NOT_FIND_START_FLAG);
- }
+ }
+ }
+ if (startRowIndex == -1) {
+ throw new BaseException(CmnCode.NOT_FIND_START_FLAG);
+ }
- //鍦ㄨ〃鏍间腑鏌ユ壘瀛愯〃瀛楁浠{$寮�澶磢}浠}缁撳熬鐨勫唴瀹�
- String regex = "\\{\\{\\$[a-zA-Z0-9_]+\\}\\}";
- Pattern pattern = Pattern.compile(regex);
- //瀛楁鎵�鍦ㄨ
- XWPFTableRow fieldRow = null;
- for (int i = startRowIndex; 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();
- Matcher matcher = pattern.matcher(text);
- if (matcher.find()) {
- fieldRow = row;
- i = rows.size();
- break;
- }
- }
- }
- if (fieldRow == null) {
- throw new BaseException(CmnCode.NOT_FIND_CHILD_TABLE_FIELD);
- }
+ //鍦ㄨ〃鏍间腑鏌ユ壘瀛愯〃瀛楁浠{$寮�澶磢}浠}缁撳熬鐨勫唴瀹�
+ String regex = "\\{\\{\\$[a-zA-Z0-9_]+\\}\\}";
+ Pattern pattern = Pattern.compile(regex);
+ //瀛楁鎵�鍦ㄨ
+ XWPFTableRow fieldRow = null;
+ for (int i = startRowIndex; 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();
+ Matcher matcher = pattern.matcher(text);
+ if (matcher.find()) {
+ fieldRow = row;
+ i = rows.size();
+ break;
+ }
+ }
+ }
+ if (fieldRow == null) {
+ throw new BaseException(CmnCode.NOT_FIND_CHILD_TABLE_FIELD);
+ }
- //璇诲彇鏈�鍚庤姣忎釜鍗曞厓鏍肩殑鍊硷紝璋冪敤getReplaceKey鏂规硶鑾峰彇琛ㄨ揪寮忎腑鐨勫��
- String[] fieldNames = new String[fieldRow.getTableCells().size()];
- for (int i = 0; i < fieldRow.getTableCells().size(); i++) {
- XWPFTableCell cell = fieldRow.getTableCells().get(i);
- String text = cell.getText();
- String replaceKey = getReplaceKey(text);
- //鑾峰彇琛ㄨ揪寮忎腑鐨勫��
- if (StringUtils.isEmpty(replaceKey)) {
- //璁剧疆鍗曞厓鏍间负绌哄��
- replaceKey = "";
- }
- fieldNames[i] = replaceKey;
- }
+ //璇诲彇鏈�鍚庤姣忎釜鍗曞厓鏍肩殑鍊硷紝璋冪敤getReplaceKey鏂规硶鑾峰彇琛ㄨ揪寮忎腑鐨勫��
+ String[] fieldNames = new String[fieldRow.getTableCells().size()];
+ for (int i = 0; i < fieldRow.getTableCells().size(); i++) {
+ XWPFTableCell cell = fieldRow.getTableCells().get(i);
+ String text = cell.getText();
+ String replaceKey = getReplaceKey(text);
+ //鑾峰彇琛ㄨ揪寮忎腑鐨勫��
+ if (StringUtils.isEmpty(replaceKey)) {
+ //璁剧疆鍗曞厓鏍间负绌哄��
+ replaceKey = "";
+ }
+ fieldNames[i] = replaceKey;
+ }
- if (subTableData != null && subTableData.size() > 0) {
- for (int i = 0; i < subTableData.size(); i++) {
- Map<String, Object> map = subTableData.get(i);
- XWPFTableRow row;
- if (i == 0) {
- row = fieldRow;
- } else {
- //鍒涘缓涓�琛屽湪fieldRowIndex涓嬮潰
- row = xwpfTable.insertNewTableRow(xwpfTable.getRows().size());
- PrintPoiUtil.copyTableRow(row, fieldRow);
- }
- //閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸
- for (int j = 0; j < fieldNames.length; j++) {
- //褰撳墠鍗曞厓鏍�
- XWPFTableCell cell = row.getCell(j);
- //娓呯┖鍗曞厓鏍煎唴瀹�
- XWPFParagraph xwpfParagraph = cell.addParagraph();
- int index = cell.getParagraphs().indexOf(xwpfParagraph);
- PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0));
- //鍒犻櫎鎵�鏈夌殑run
- for (int k = xwpfParagraph.getRuns().size() - 1; k >= 0; k--) {
- xwpfParagraph.removeRun(k);
- }
- XWPFRun xwpfRun = xwpfParagraph.createRun();
- PrintPoiUtil.copyRun(xwpfRun, cell.getParagraphs().get(0).getRuns().get(0));
- //鍒犻櫎cell涓殑娈佃惤浣嗘帓闄ゆ柊澧炵殑娈佃惤
- for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) {
- if (k < index) {
- cell.removeParagraph(k);
- }
- }
- //鍒ゆ柇鏄惁鏄簭鍙峰垪
- if (indexKey.equals(fieldNames[j])) {
- cell.setText(String.valueOf(i + 1));
- continue;
- }
- Object value = map.get(fieldNames[j]);
- if (value == null) {
- value = "";
- }
- xwpfRun.setText(value.toString(), 0);
- if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldNames[j]) && !StringUtils.isEmpty(map.get("sign_attachment_uuid"))) {
- //鎰忚妗� 鎻掑叆绛惧悕鍥剧墖鍒板崟鍏冩牸鍙充笅瑙掍綅缃�
- xwpfParagraph = cell.addParagraph();
- xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT);
- XWPFRun run = xwpfParagraph.createRun();
- String signAttachmentBase64 = map.get("sign_attachment_uuid").toString();
- //灏哹ase64杞崲瀛楄妭娴�
- byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]);
- //灏嗗瓧鑺傛祦杞崲涓鸿緭鍏ユ祦
- InputStream inputStream = new ByteArrayInputStream(bytes);
- try {
- //鎹㈣鎻掑叆鍥剧墖
- run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20));
- inputStream.close();
- } catch (InvalidFormatException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
+ if (subTableData != null && subTableData.size() > 0) {
+ 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) {
+ row = fieldRow;
+ } else {
+ int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow);
+ //鍒涘缓涓�琛屽湪fieldRowIndex涓嬮潰
+ 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);
+ //娓呯┖鍗曞厓鏍煎唴瀹�
+ XWPFParagraph xwpfParagraph = cell.addParagraph();
+ int index = cell.getParagraphs().indexOf(xwpfParagraph);
+ PrintPoiUtil.copyParagraph(xwpfParagraph, cell.getParagraphs().get(0));
+ //璁剧疆娈佃惤鏂囧瓧瀵归綈鏂瑰紡
+ xwpfParagraph.setAlignment(cell.getParagraphs().get(0).getAlignment());
+ //鍒犻櫎鎵�鏈夌殑run
+ for (int k = xwpfParagraph.getRuns().size() - 1; k >= 0; k--) {
+ xwpfParagraph.removeRun(k);
+ }
+ XWPFRun xwpfRun = xwpfParagraph.createRun();
+ PrintPoiUtil.copyRun(xwpfRun, cell.getParagraphs().get(0).getRuns().get(0));
+ //鍒犻櫎cell涓殑娈佃惤浣嗘帓闄ゆ柊澧炵殑娈佃惤
+ for (int k = cell.getParagraphs().size() - 1; k >= 0; k--) {
+ if (k < index) {
+ cell.removeParagraph(k);
+ }
+ }
+ //鍒ゆ柇鏄惁鏄簭鍙峰垪
+ if (indexKey.equals(fieldName)) {
+ if (map.isEmpty()) {
+ xwpfRun.setText("", 0);
+ } else {
+ xwpfRun.setText(String.valueOf(i + rowCount), 0);
+ }
+ continue;
+ }
+ Object value = map.get(fieldName);
+ if (value == null) {
+ value = "";
+ }
+ xwpfRun.setText(value.toString(), 0);
+ if ("lx_flow_opinion".equals(this.replaceKey) && "opinion".equals(fieldName)) {
+ if (!StringUtils.isEmpty(map.get("sign_attachment_uuid"))) {
+ //鎰忚妗� 鎻掑叆绛惧悕鍥剧墖鍒板崟鍏冩牸鍙充笅瑙掍綅缃�
+ xwpfParagraph = cell.addParagraph();
+ xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT);
+ XWPFRun run = xwpfParagraph.createRun();
+ String signAttachmentBase64 = map.get("sign_attachment_uuid").toString();
+ //灏哹ase64杞崲瀛楄妭娴�
+ byte[] bytes = Base64.getDecoder().decode(signAttachmentBase64.split(",")[1]);
+ //灏嗗瓧鑺傛祦杞崲涓鸿緭鍏ユ祦
+ InputStream inputStream = new ByteArrayInputStream(bytes);
+ try {
+ //鎹㈣鎻掑叆鍥剧墖
+ run.addPicture(inputStream, XWPFDocument.PICTURE_TYPE_PNG, "sign.png", Units.toEMU(50), Units.toEMU(20));
+ run.addBreak();
+ run.setText("" + map.get("deal_time").toString().substring(0, 16));
+ inputStream.close();
+ } catch (InvalidFormatException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ } else {
+ xwpfParagraph = cell.addParagraph();
+ xwpfParagraph.setAlignment(ParagraphAlignment.RIGHT);
+ XWPFRun run = xwpfParagraph.createRun();
+ run.setText((String) map.get("actual_person"));
+ run.addBreak();
+ run.setText(map.get("deal_time").toString().substring(0, 16));
+ }
- }
- }
- } else {
+ }
+
+ }
+ }
+ } else {
// 鍒犻櫎fieldRow
- int index = xwpfTable.getRows().indexOf(fieldRow);
- xwpfTable.removeRow(index);
- }
- //鍒犻櫎璧峰琛�
- xwpfTable.removeRow(startRowIndex);
+ int index = xwpfTable.getRows().indexOf(fieldRow);
+ xwpfTable.removeRow(index);
+ }
+ //鍒犻櫎璧峰琛�
+ xwpfTable.removeRow(startRowIndex);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
}
/**
--
Gitblit v1.9.2