From cfd0903dc0cfc7ffd39c2caa101f31a50441d39c Mon Sep 17 00:00:00 2001
From: 6c <420680616@qq.com>
Date: 星期五, 31 十月 2025 17:29:59 +0800
Subject: [PATCH] 已送审保养打印; 列表批量打印
---
doc/print.md | 2
src/main/java/com/product/print/service/ide/IPrintRealizeService.java | 7
src/main/java/com/product/print/util/DynamicTableRenderPolicy.java | 313 ++++++++++++++++--------------
src/main/java/com/product/print/config/CmnCode.java | 3
src/main/java/com/product/print/util/CustomPictureRenderPolicy.java | 31 ++
src/main/java/com/product/print/service/PrintRealizeService.java | 235 ++++++++++++++---------
6 files changed, 345 insertions(+), 246 deletions(-)
diff --git a/doc/print.md b/doc/print.md
index eaf3fe2..2ce3aa6 100644
--- a/doc/print.md
+++ b/doc/print.md
@@ -26,7 +26,7 @@
### 瀛愯〃鏍囩
- {{$~index~}}锛� 鍥哄畾鍐欐硶锛岃緭鍑哄瓙琛ㄨ鍙�
+ {{$~i~}}锛� 鍥哄畾鍐欐硶锛岃緭鍑哄瓙琛ㄨ鍙�
{{$瀛楁鍚峿}锛氳緭鍑哄瓙琛ㄤ腑鎸囧畾瀛楁鐨勫��
### 
diff --git a/src/main/java/com/product/print/config/CmnCode.java b/src/main/java/com/product/print/config/CmnCode.java
index 6d901df..39c45c3 100644
--- a/src/main/java/com/product/print/config/CmnCode.java
+++ b/src/main/java/com/product/print/config/CmnCode.java
@@ -32,6 +32,9 @@
NOT_FIND_START_FLAG("琛ㄦ牸涓病鏈夋壘鍒拌捣濮嬩笅鏍�", 15),
//瀛愯〃鏍间腑娌℃湁鎵惧埌瀵瑰簲鐨勫瓧娈�
NOT_FIND_CHILD_TABLE_FIELD("瀛愯〃鏍间腑娌℃湁鎵惧埌瀵瑰簲鐨勫瓧娈�", 16),
+ GET_BEAN_FAIL("鑾峰彇bean澶辫触", 17),
+ DEALT_FILE_PATH_IS_EMPTY("澶勭悊鍚庣殑鏂囦欢璺緞涓虹┖", 18),
+ EXISTS_FILE_RECORD_BUT_NO_FILE("瀛樺湪鏂囦欢璁板綍锛屼絾鏄湭鎵惧埌瀵瑰簲鏂囦欢", 19),
;
private String text;
diff --git a/src/main/java/com/product/print/service/PrintRealizeService.java b/src/main/java/com/product/print/service/PrintRealizeService.java
index 3839b51..4c7feda 100644
--- a/src/main/java/com/product/print/service/PrintRealizeService.java
+++ b/src/main/java/com/product/print/service/PrintRealizeService.java
@@ -10,10 +10,11 @@
import com.deepoove.poi.config.ConfigureBuilder;
import com.deepoove.poi.data.TextRenderData;
import com.deepoove.poi.data.style.Style;
-import com.deepoove.poi.render.RenderContext;
+import com.google.common.collect.Lists;
import com.product.common.lang.StringUtils;
import com.product.core.cache.DataPoolCacheImpl;
import com.product.core.config.Global;
+import com.product.core.entity.DataEntity;
import com.product.core.entity.DataTableEntity;
import com.product.core.entity.FieldSetEntity;
import com.product.core.exception.BaseException;
@@ -30,17 +31,15 @@
import com.product.tool.flow.service.FlowDetailService;
import com.product.util.BaseUtil;
import com.product.util.SystemParamReplace;
-import org.apache.poi.xwpf.usermodel.XWPFTable;
+import org.apache.pdfbox.io.MemoryUsageSetting;
+import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.OutputStream;
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-import java.util.UUID;
+import java.util.*;
import java.util.stream.Collectors;
@@ -121,17 +120,32 @@
/**
* 鎵撳嵃浼犺緭pdf娴佸埌鍓嶇
*
- * @param fse 鎵撳嵃鏁版嵁
+ * @param fseOrDte 鎵撳嵃鏁版嵁
* @param response 鍝嶅簲
* @param isConvertPdf 鏄惁杞崲涓簆df
* @throws BaseException
*/
@Override
- public void print(FieldSetEntity fse, HttpServletResponse response, boolean isConvertPdf) throws BaseException {
- //鎵撳嵃閰嶇疆
- FieldSetEntity printConfig = getPrintConfig(fse.getString("~" + CmnConst.PRINT_TEMP + "~"));
+ public void print(DataEntity fseOrDte, HttpServletResponse response, boolean isConvertPdf) throws BaseException {
+ FieldSetEntity fse;
+ DataTableEntity dte;
+ //鎵撳嵃閰嶇疆
+ FieldSetEntity printConfig;
+ if (fseOrDte.isFieldsetEntity()) {
+ fse = (FieldSetEntity) fseOrDte;
+ printConfig = getPrintConfig(getPrintTemplateField(fse));
+ } else if (fseOrDte.isDataTableEntity()) {
+ dte = (DataTableEntity) fseOrDte;
+ fse = dte.getFieldSetEntity(0);
+ printConfig = getPrintConfig(getPrintTemplateField(fse));
+ } else {
+ throw new BaseException(CmnCode.GET_BEAN_FAIL);
+ }
//鑾峰彇鍒版浛鎹㈠悗鐨勬枃浠惰矾寰� 锛坧df鏂囦欢鎴栬�厀ord鏂囦欢锛�
- String tempPdfFilePath = replaceTemplateFileOut(printConfig, fse, isConvertPdf);
+ String tempPdfFilePath = replaceTemplateFileOut(printConfig, fseOrDte, isConvertPdf);
+ if (StringUtils.isEmpty(tempPdfFilePath)) {
+ throw new BaseException(CmnCode.DEALT_FILE_PATH_IS_EMPTY);
+ }
//鑾峰彇鏂囦欢鍚�
String fileName = BaseUtil.ifNull(printConfig.getString(CmnConst.PRINT_FILE_NAME), printConfig.getString(CmnConst.PRINT_NAME));
if (isConvertPdf) {
@@ -146,11 +160,27 @@
// 璁剧疆鑷畾涔夊ごContent-Disposition
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncodeUtil.encode(fileName));
- try (
- OutputStream out = response.getOutputStream();
- ) {
- FileUtil.writeToStream(new File(tempPdfFilePath), out);
- } catch (Exception e) {
+ try (OutputStream out = response.getOutputStream();) {
+ if (tempPdfFilePath.contains(",")) {
+ // 鏇撮珮鏁堢殑鏂瑰紡锛氱洿鎺ヤ粠鏂囦欢鍚堝苟鍒拌緭鍑烘祦
+ PDFMergerUtility merger = new PDFMergerUtility();
+ merger.setDestinationStream(response.getOutputStream());
+
+ String[] tempPdfFilePathArr = tempPdfFilePath.split(",");
+ for (int i = 0; i < tempPdfFilePathArr.length; i++) {
+ String singleTempPdfFilePath = tempPdfFilePathArr[i];
+ File file = new File(singleTempPdfFilePath);
+ if (file.exists()) {
+ merger.addSource(file); // 鐩存帴娣诲姞鏂囦欢婧�
+ }
+ }
+
+ // 鎵ц鍚堝苟
+ merger.mergeDocuments(MemoryUsageSetting.setupMainMemoryOnly());
+ } else {
+ FileUtil.writeToStream(new File(tempPdfFilePath), out);
+ }
+ } catch (Exception e) {
e.printStackTrace();
throw new BaseException(CmnCode.PRINT_CONTENT_FAIL, e);
} finally {
@@ -159,8 +189,23 @@
System.out.println(absolutePath);
file.delete();
}
-
}
+
+ /**
+ * 鑾峰彇鎵撳嵃妯℃澘瀛楁
+ * @param fse
+ * @return
+ */
+ private String getPrintTemplateField(FieldSetEntity fse) {
+ if (FieldSetEntity.isEmpty(fse)) {
+ return null;
+ }
+ String printTemplateField = fse.getString("~" + CmnConst.PRINT_TEMP + "~");
+ if (StringUtils.isEmpty(printTemplateField)) {
+ printTemplateField = fse.getString("~print_template~");
+ }
+ return printTemplateField;
+ }
/**
* 鑾峰彇鎵撳嵃閰嶇疆
@@ -183,75 +228,97 @@
* 鏇挎崲妯℃澘鏂囦欢骞惰緭鍑�
*
* @param printConf 鎵撳嵃閰嶇疆
- * @param fse 鏇挎崲鏁版嵁
+ * @param fseOrDte 鏇挎崲鏁版嵁
* @param isConvertPdf 鏄惁杞崲涓簆df
* @return 鏇挎崲鍚庣殑鏂囦欢璺緞
* @throws BaseException 寮傚父
*/
- private String replaceTemplateFileOut(FieldSetEntity printConf, FieldSetEntity fse, boolean isConvertPdf) throws BaseException {
+ private String replaceTemplateFileOut(FieldSetEntity printConf, DataEntity fseOrDte, boolean isConvertPdf) throws BaseException {
+ FieldSetEntity fse = null;
+ DataTableEntity dte = null;
+ if (fseOrDte.isFieldsetEntity()) {
+ fse = (FieldSetEntity) fseOrDte;
+ } else if (fseOrDte.isDataTableEntity()) {
+ dte = (DataTableEntity) fseOrDte;
+ } else {
+ throw new BaseException(CmnCode.GET_BEAN_FAIL);
+ }
+ if (fse != null) {
+ dte = new DataTableEntity();
+ dte.addFieldSetEntity(fse);
+ }
- Object[] fields = fse.getFields();
- for (int i = 0; i < fields.length; i++) {
- String field = fields[i].toString();
- FieldSetEntity metaEntity = fse.getMeta().getFieldMeta(field);
- if (metaEntity == null) {
- continue;
- }
- String fieldType = metaEntity.getString("field_type");
- //鍒ゆ柇鏄惁鎷ユ湁娴佺▼鏍囪瘑
- if ("flowsign".equals(fieldType)) {
- if (StringUtils.equalsAny(fse.getString(field), "1", "2")) {
- //娴佺▼鍔炵悊涓垨鍔炵粨 鑾峰彇娴佺▼鎰忚
- FieldSetEntity flowTask = getBaseDao().getFieldSetByFilter("product_sys_flow_task", "table_name=? and record_uuid=?", new Object[]{fse.getTableName(), fse.getUUID()}, false);
- if (flowTask != null) {
- String taskUuid = flowTask.getString(CmnConst.UUID);
- //鑾峰彇娴佺▼鎰忚
- FlowDetailService flowDetailService = SpringUtil.getBean(FlowDetailService.class);
- JSONArray opinion = flowDetailService.getHistoryInfo(taskUuid);
- fse.setValue("~flow_opinion~", opinion);
- }
- }
- break;
- }
- }
+ List<String> localTempPathList = Lists.newArrayList();
+ if (!DataTableEntity.isEmpty(dte)) {
+ for (int m = 0; m < dte.getRows(); m++) {
+ fse = dte.getFieldSetEntity(m);
- // 鎵撳嵃妯℃澘闄勪欢uuid
- String template_uuid = printConf.getString(CmnConst.PRINT_TEMPLATE);
- // 鑾峰彇鎵撳嵃妯℃澘
- File file = getTemplateFile(template_uuid);
- // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭��
- getBaseDao().loadPromptData(fse);
- // 鏂囦欢鍚嶅墠缂�閮ㄥ垎
- Object tempKey = UUID.randomUUID();
- // 鏇挎崲鍚庣殑word涓存椂璺緞
- String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx";
+ Object[] fields = fse.getFields();
+ for (int i = 0; i < fields.length; i++) {
+ String field = fields[i].toString();
+ FieldSetEntity metaEntity = fse.getMeta().getFieldMeta(field);
+ if (metaEntity == null) {
+ continue;
+ }
+ String fieldType = metaEntity.getString("field_type");
+ //鍒ゆ柇鏄惁鎷ユ湁娴佺▼鏍囪瘑
+ if ("flowsign".equals(fieldType)) {
+ if (StringUtils.equalsAny(fse.getString(field), "1", "2")) {
+ //娴佺▼鍔炵悊涓垨鍔炵粨 鑾峰彇娴佺▼鎰忚
+ FieldSetEntity flowTask = getBaseDao().getFieldSetByFilter("product_sys_flow_task", "table_name=? and record_uuid=?", new Object[]{fse.getTableName(), fse.getUUID()}, false);
+ if (flowTask != null) {
+ String taskUuid = flowTask.getString(CmnConst.UUID);
+ //鑾峰彇娴佺▼鎰忚
+ FlowDetailService flowDetailService = SpringUtil.getBean(FlowDetailService.class);
+ JSONArray opinion = flowDetailService.getHistoryInfo(taskUuid);
+ fse.setValue("~flow_opinion~", opinion);
+ }
+ }
+ break;
+ }
+ }
- replaceWord(localTempPathWord, file.getPath(), fse);
- file.delete();
+ // 鎵撳嵃妯℃澘闄勪欢uuid
+ String template_uuid = printConf.getString(CmnConst.PRINT_TEMPLATE);
+ // 鑾峰彇鎵撳嵃妯℃澘
+ File file = getTemplateFile(template_uuid);
+ // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭��
+ getBaseDao().loadPromptData(fse);
+ // 鏂囦欢鍚嶅墠缂�閮ㄥ垎
+ Object tempKey = UUID.randomUUID();
+ // 鏇挎崲鍚庣殑word涓存椂璺緞
+ String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx";
- String replaceParams = SystemParamReplace.replaceParams(Optional.ofNullable(printConf.getString(CmnConst.PRINT_FILE_NAME)).orElse(printConf.getString(CmnConst.PRINT_NAME)), fse);
- printConf.setValue(CmnConst.PRINT_FILE_NAME, replaceParams);
- if (isConvertPdf) {
- try {
- // 鏇挎崲鍚庣殑pdf涓存椂璺緞
- String localTempPathPdf = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".pdf";
- //妫�鏌ユ枃浠舵槸鍚﹀瓨鍦ㄤ笉瀛樺垯鍒涘缓
- FileUtil.touch(localTempPathPdf);
- // 杞崲pdf
- PdfConcurrenceUtil.convertToPdf(localTempPathWord, localTempPathPdf, "docx");
- // 鍒犻櫎word涓存椂鏂囦欢
- File wordTemp = new File(localTempPathWord);
- if (wordTemp.exists()) {
- wordTemp.delete();
- }
- return localTempPathPdf;
- } catch (Exception e) {
- throw new BaseException(CmnCode.CONVERT_PDF_ERROR, e);
- } finally {
- FileUtil.del(localTempPathWord);
- }
- }
- return localTempPathWord;
+ replaceWord(localTempPathWord, file.getPath(), fse);
+ file.delete();
+
+ String replaceParams = SystemParamReplace.replaceParams(Optional.ofNullable(printConf.getString(CmnConst.PRINT_FILE_NAME)).orElse(printConf.getString(CmnConst.PRINT_NAME)), fse);
+ printConf.setValue(CmnConst.PRINT_FILE_NAME, replaceParams);
+ if (isConvertPdf) {
+ try {
+ // 鏇挎崲鍚庣殑pdf涓存椂璺緞
+ String localTempPathPdf = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".pdf";
+ //妫�鏌ユ枃浠舵槸鍚﹀瓨鍦ㄤ笉瀛樺垯鍒涘缓
+ FileUtil.touch(localTempPathPdf);
+ // 杞崲pdf
+ PdfConcurrenceUtil.convertToPdf(localTempPathWord, localTempPathPdf, "docx");
+ // 鍒犻櫎word涓存椂鏂囦欢
+ File wordTemp = new File(localTempPathWord);
+ if (wordTemp.exists()) {
+ wordTemp.delete();
+ }
+ localTempPathList.add(localTempPathPdf);
+ } catch (Exception e) {
+ throw new BaseException(CmnCode.CONVERT_PDF_ERROR, e);
+ } finally {
+ FileUtil.del(localTempPathWord);
+ }
+ } else {
+ localTempPathList.add(localTempPathWord);
+ }
+ }
+ }
+ return BaseUtil.collection2String(localTempPathList);
}
/**
@@ -274,18 +341,6 @@
Map<String, DataTableEntity> subDataMap = dataFse.getSubData();
ConfigureBuilder config = Configure.builder();
config.addPlugin('@', new FlowOpinionRenderPolicy(flowOpinion));
- config.addPlugin('$', new com.deepoove.poi.policy.DynamicTableRenderPolicy() {
- @Override
- public void doRender(RenderContext<Object> context) throws Exception {
- return;
- }
-
- @Override
- public void render(XWPFTable table, Object data) throws Exception {
- return;
- }
- });
-
config.addPlugin('&', new CustomPictureRenderPolicy());
config.buildGrammerRegex("(#)?([\\w\\u4e00-\\u9fa5]+)(\\.?[\\w\\u4e00-\\u9fa5\\|]*)*(#)?");
TableEmptyHandler tableEmptyHandler = new TableEmptyHandler();
diff --git a/src/main/java/com/product/print/service/ide/IPrintRealizeService.java b/src/main/java/com/product/print/service/ide/IPrintRealizeService.java
index 3b5b4d2..9b8f6f4 100644
--- a/src/main/java/com/product/print/service/ide/IPrintRealizeService.java
+++ b/src/main/java/com/product/print/service/ide/IPrintRealizeService.java
@@ -1,6 +1,6 @@
package com.product.print.service.ide;
-import com.product.core.entity.FieldSetEntity;
+import com.product.core.entity.DataEntity;
import com.product.core.exception.BaseException;
import javax.servlet.http.HttpServletResponse;
@@ -16,11 +16,10 @@
/**
* 鎵撳嵃
- * @param fse 鎵撳嵃鏁版嵁
+ * @param fseOrDte 鎵撳嵃鏁版嵁
* @param response 鍝嶅簲
* @param isConvertPdf 鏄惁杞崲涓簆df
* @throws BaseException
*/
- void print(FieldSetEntity fse, HttpServletResponse response,boolean isConvertPdf) throws BaseException;
-
+ void print(DataEntity fseOrDte, HttpServletResponse response, boolean isConvertPdf) throws BaseException;
}
diff --git a/src/main/java/com/product/print/util/CustomPictureRenderPolicy.java b/src/main/java/com/product/print/util/CustomPictureRenderPolicy.java
index de58bd3..a95664c 100644
--- a/src/main/java/com/product/print/util/CustomPictureRenderPolicy.java
+++ b/src/main/java/com/product/print/util/CustomPictureRenderPolicy.java
@@ -8,9 +8,12 @@
import com.deepoove.poi.data.Pictures;
import com.deepoove.poi.policy.PictureRenderPolicy;
import com.deepoove.poi.render.RenderContext;
+import com.product.common.lang.StringUtils;
import com.product.core.dao.BaseDao;
import com.product.core.entity.FieldSetEntity;
+import com.product.core.spring.context.SpringMVCContextHolder;
import com.product.file.service.FileManagerService;
+import com.product.print.config.CmnCode;
import java.io.File;
import java.io.FileInputStream;
@@ -31,9 +34,29 @@
public PictureRenderData cast(Object source) throws Exception {
FileManagerService fileManagerService = SpringUtil.getBean(FileManagerService.class);
String templateUid = source.toString();
+ if (StringUtils.isEmpty(templateUid)) {
+ return null;
+ }
+ int width = 0;
+ int height = 0;
+ if (templateUid.contains(" ")) {
+ String[] infoArr = templateUid.split(" ");
+ templateUid = infoArr[0];
+ width = Integer.parseInt(infoArr[1]);
+ height = Integer.parseInt(infoArr[2]);
+ }
BaseDao baseDao = SpringUtil.getBean(BaseDao.class);
FieldSetEntity attachmentFse = baseDao.getFieldSetEntity("product_sys_attachments", templateUid, false);
- fileManagerService.getFile(templateUid);
+
+ // 閬囬敊璺宠繃锛岄粯璁や娇鐢ㄩ�忔槑鑳屾櫙涓嶆嫤鎴紝璁板綍鏃ュ織鍒版枃浠�
+ try {
+ fileManagerService.getFile(templateUid);
+ } catch (Exception e) {
+ attachmentFse = baseDao.getFieldSetEntityByFilter("product_sys_attachments", "attachment_title=?", new Object[]{"sys_sp_deal_閫忔槑鑳屾櫙"}, false);
+ fileManagerService.getFile(attachmentFse.getUUID());
+ SpringMVCContextHolder.getSystemLogger().error(String.format("%s-闄勪欢uuid:%s", CmnCode.EXISTS_FILE_RECORD_BUT_NO_FILE.getText(), templateUid));
+ }
+
String fileName = attachmentFse.getString("file_name");
String type = fileName.substring(fileName.lastIndexOf(".") + 1);
File file = fileManagerService.getFile(attachmentFse);
@@ -47,7 +70,11 @@
} else if ("svg".equals(type)) {
imgType = PictureType.SVG;
}
- return converter.convert(Pictures.ofStream(new FileInputStream(file), imgType).create());
+ if (width > 0 && height > 0) {
+ return converter.convert(Pictures.ofStream(new FileInputStream(file), imgType).size(width, height).create());
+ } else {
+ return converter.convert(Pictures.ofStream(new FileInputStream(file), imgType).create());
+ }
}
@Override
diff --git a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java
index 1dd73db..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,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,159 +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 {
- int fieldRowIndex = xwpfTable.getRows().indexOf(fieldRow);
- //鍒涘缓涓�琛屽湪fieldRowIndex涓嬮潰
- row = xwpfTable.insertNewTableRow(fieldRowIndex + i);
- 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));
- //璁剧疆娈佃惤鏂囧瓧瀵归綈鏂瑰紡
- 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(fieldNames[j])) {
- cell.setText(String.valueOf(i + 1));
- continue;
- }
- Object value = map.get(fieldNames[j]);
- 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 (!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));
- }
+ 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