From 69da583c3a1ebb923c5023c5e2b42f094ccb1b53 Mon Sep 17 00:00:00 2001 From: 许鹏程 <1821349743@qq.com> Date: 星期四, 29 六月 2023 09:32:35 +0800 Subject: [PATCH] poi、easyexcel、poi-tl升级 ,合并空调中的报表、数据源模块 --- src/main/java/com/product/print/config/CmnConst.java | 38 +- src/main/java/com/product/print/service/ide/IPrintRealizeService.java | 11 /dev/null | 145 -------- src/main/java/com/product/print/util/DynamicTableRenderPolicy.java | 177 +++++++++ src/main/java/com/product/print/config/CmnCode.java | 6 pom.xml | 39 -- src/main/java/com/product/print/service/PrintRealizeService.java | 506 +++++++++++---------------- src/main/java/com/product/print/controller/PrintRealizeController.java | 102 ++-- 8 files changed, 475 insertions(+), 549 deletions(-) diff --git a/pom.xml b/pom.xml index 1d241b3..98feb5c 100644 --- a/pom.xml +++ b/pom.xml @@ -33,47 +33,10 @@ <groupId>com.lx</groupId> <artifactId>product-server-admin</artifactId> </dependency> - <!-- <dependency>--> - <!-- <groupId>org.apache.poi</groupId>--> - <!-- <artifactId>poi</artifactId>--> - <!-- <version>5.1.0</version>--> - <!-- </dependency>--> - <!-- <!– https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml –>--> - <!-- <dependency>--> - <!-- <groupId>org.apache.poi</groupId>--> - <!-- <artifactId>poi-ooxml</artifactId>--> - <!-- <version>5.1.0</version>--> - <!-- </dependency>--> <dependency> <groupId>com.deepoove</groupId> <artifactId>poi-tl</artifactId> - <version>1.4.2</version> - <exclusions> - <exclusion> - <groupId>org.apache.poi</groupId> - <artifactId>poi</artifactId> - </exclusion> - <exclusion> - <groupId>org.apache.poi</groupId> - <artifactId>poi-ooxml</artifactId> - </exclusion> - <exclusion> - <groupId>org.apache.poi</groupId> - <artifactId>poi-ooxml-schemas</artifactId> - </exclusion> - <exclusion> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-api</artifactId> - </exclusion> - <exclusion> - <groupId>log4j</groupId> - <artifactId>log4j</artifactId> - </exclusion> - <exclusion> - <groupId>org.slf4j</groupId> - <artifactId>slf4j-log4j12</artifactId> - </exclusion> - </exclusions> + <version>1.10.5</version> </dependency> </dependencies> </project> diff --git a/src/main/java/com/product/print/config/CmnCode.java b/src/main/java/com/product/print/config/CmnCode.java index c5c992a..978871c 100644 --- a/src/main/java/com/product/print/config/CmnCode.java +++ b/src/main/java/com/product/print/config/CmnCode.java @@ -24,6 +24,12 @@ PRINT_CONFIG_NOT_EXIST("妯℃澘閰嶇疆涓嶅瓨鍦�", 11), //鍔ㄦ�佽〃鏍艰缃敊璇� DYNAMIC_TABLE_SETTING_ERROR("鍔ㄦ�佽〃鏍艰缃敊璇�", 12), + //鏇挎崲妯℃澘鍐呭閿欒 + REPLACE_TEMPLATE_CONTENT_ERROR("鏇挎崲妯℃澘鍐呭閿欒", 13), + //杞崲pdf閿欒 + CONVERT_PDF_ERROR("杞崲pdf閿欒", 14), + //鏍间腑娌℃湁鎵惧埌缁撴潫鏍囪瘑{{$~end~}} + NOT_FIND_END_FLAG("鏍间腑娌℃湁鎵惧埌缁撴潫鏍囪瘑{{$~end~}}", 15), ; private String text; diff --git a/src/main/java/com/product/print/config/CmnConst.java b/src/main/java/com/product/print/config/CmnConst.java index 2391aae..d7ef0cd 100644 --- a/src/main/java/com/product/print/config/CmnConst.java +++ b/src/main/java/com/product/print/config/CmnConst.java @@ -1,32 +1,30 @@ package com.product.print.config; +import com.product.core.config.CoreConst; + /** * @ClassName CmnConst * @Description 鎵撳嵃妯″潡甯搁噺 * @Author cheng * @Date 2021/11/30 15:39 */ -public class CmnConst { +public class CmnConst extends CoreConst { - public static final String CPAGE = "cpage"; // 椤垫暟 - public static final String PAGESIZE = "pagesize"; // 姣忛〉鏉℃暟 - public static final String UUID = "uuid"; + public static final String PRINT_TEMP = "print_temp"; + public static final String PRINT_TEMPLATE = "print_template"; + //鎵撳嵃閰嶇疆琛� + public static final String TABLE_PRINT_CONFIG = "product_sys_print_config"; - public static final String PRINT_TEMP = "print_temp"; - public static final String PRINT_TEMPLATE = "print_template"; - //鎵撳嵃閰嶇疆琛� - public static final String TABLE_PRINT_CONFIG = "product_sys_print_config"; - - public static final String PRINT_NAME="print_name"; - public static final String PRINT_FILE_NAME="print_file_name"; - - - public static final String DICT_VALUE="dict_value"; - public static final String DICT_LABEL="dict_label"; - public static final String FIELD_REFERECE="field_reference"; - public static final String _SAVE_VALUE="_save_value"; - public static final String PRINT_FONT="Wingdings 2"; - public static final String PRINT_CHECKED_CHAR="鈽�"; - public static final String PRINT_UNCHECKED_CHAR="鈻�"; + public static final String PRINT_NAME = "print_name"; + public static final String PRINT_FILE_NAME = "print_file_name"; + + + public static final String DICT_VALUE = "dict_value"; + public static final String DICT_LABEL = "dict_label"; + public static final String FIELD_REFERECE = "field_reference"; + public static final String _SAVE_VALUE = "_save_value"; + public static final String PRINT_FONT = "Wingdings 2"; + public static final String PRINT_CHECKED_CHAR = "鈽�"; + public static final String PRINT_UNCHECKED_CHAR = "鈻�"; } diff --git a/src/main/java/com/product/print/controller/PrintRealizeController.java b/src/main/java/com/product/print/controller/PrintRealizeController.java index 6a7e876..8dc8918 100644 --- a/src/main/java/com/product/print/controller/PrintRealizeController.java +++ b/src/main/java/com/product/print/controller/PrintRealizeController.java @@ -28,63 +28,63 @@ @RequestMapping("/api/print/realize") public class PrintRealizeController extends AbstractBaseController { - @Autowired - IPrintRealizeService printRealizeService; + @Autowired + IPrintRealizeService printRealizeService; - @PostMapping("/print/{version}") - @ApiVersion(1) - public String print(HttpServletRequest request, HttpServletResponse response) { - try { - FieldSetEntity fse = null; - Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); - if (bean != null) { - RequestParameterEntity reqp = (RequestParameterEntity) bean; - fse = reqp.getFormData(); - } - if (bean == null || fse == null) { - return this.error(CmnCode.SYSTEM_FORM_NODATA); - } + @PostMapping("/print/{version}") + @ApiVersion(1) + public String print(HttpServletRequest request, HttpServletResponse response) { + try { + FieldSetEntity fse = null; + Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); + if (bean != null) { + RequestParameterEntity reqp = (RequestParameterEntity) bean; + fse = reqp.getFormData(); + } + if (bean == null || fse == null) { + return this.error(CmnCode.SYSTEM_FORM_NODATA); + } // if (!CmnConst.TABLE_PRINT_CONFIG.equals(fse.getTableName())) { // return error(CmnCode.SYSTEM_TABLE_NODATA); // } - if (StringUtils.isEmpty(CmnConst.PRINT_TEMP)) { - return error(CmnCode.SYSTEM_FORM_COUNT); - } - printRealizeService.print(fse, response); - return OK(); - } catch (BaseException e) { - return error(e); - } catch (Exception e) { - return error(CmnCode.GET_PRINT_CONFIG_LIST_FAIL, e); - } - } - - @PostMapping("/printWord/{version}") - @ApiVersion(1) - public String printWord(HttpServletRequest request, HttpServletResponse response) { - try { - FieldSetEntity fse = null; - Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); - if (bean != null) { - RequestParameterEntity reqp = (RequestParameterEntity) bean; - fse = reqp.getFormData(); - } - if (bean == null || fse == null) { - return this.error(CmnCode.SYSTEM_FORM_NODATA); - } + if (StringUtils.isEmpty(CmnConst.PRINT_TEMP)) { + return error(CmnCode.SYSTEM_FORM_COUNT); + } + printRealizeService.print(fse, response, true); + return OK(); + } catch (BaseException e) { + return error(e); + } catch (Exception e) { + return error(CmnCode.GET_PRINT_CONFIG_LIST_FAIL, e); + } + } + + @PostMapping("/printWord/{version}") + @ApiVersion(1) + public String printWord(HttpServletRequest request, HttpServletResponse response) { + try { + FieldSetEntity fse = null; + Object bean = request.getAttribute(CoreConst.API_POST_REQUEST_DATA); + if (bean != null) { + RequestParameterEntity reqp = (RequestParameterEntity) bean; + fse = reqp.getFormData(); + } + if (bean == null || fse == null) { + return this.error(CmnCode.SYSTEM_FORM_NODATA); + } // if (!CmnConst.TABLE_PRINT_CONFIG.equals(fse.getTableName())) { // return error(CmnCode.SYSTEM_TABLE_NODATA); // } - if (StringUtils.isEmpty(CmnConst.PRINT_TEMP)) { - return error(CmnCode.SYSTEM_FORM_COUNT); - } - printRealizeService.printWord(fse, response); - return OK(); - } catch (BaseException e) { - return error(e); - } catch (Exception e) { - return error(CmnCode.GET_PRINT_CONFIG_LIST_FAIL, e); - } - } + if (StringUtils.isEmpty(CmnConst.PRINT_TEMP)) { + return error(CmnCode.SYSTEM_FORM_COUNT); + } + printRealizeService.print(fse, response, false); + return OK(); + } catch (BaseException e) { + return error(e); + } catch (Exception e) { + return error(CmnCode.GET_PRINT_CONFIG_LIST_FAIL, e); + } + } } diff --git a/src/main/java/com/product/print/service/HackLoopTableRenderPolicy.java b/src/main/java/com/product/print/service/HackLoopTableRenderPolicy.java deleted file mode 100644 index d270bc0..0000000 --- a/src/main/java/com/product/print/service/HackLoopTableRenderPolicy.java +++ /dev/null @@ -1,145 +0,0 @@ -package com.product.print.service; - - -import cn.hutool.core.collection.CollectionUtil; -import com.deepoove.poi.NiceXWPFDocument; -import com.deepoove.poi.XWPFTemplate; -import com.deepoove.poi.policy.RenderPolicy; -import com.deepoove.poi.template.ElementTemplate; -import com.deepoove.poi.template.run.RunTemplate; -import com.product.common.lang.StringUtils; -import org.apache.poi.xwpf.usermodel.*; -import org.apache.xmlbeans.XmlCursor; -import org.apache.xmlbeans.XmlObject; -import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTTbl; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.regex.Matcher; -import java.util.regex.Pattern; - -/** - * @author cheng - * @desc poi-tl 鑷畾涔夊瓧琛ㄦ覆鏌� - * @date 2022骞�5鏈�6鏃�17:04:31 - */ -public class HackLoopTableRenderPolicy implements RenderPolicy { - - @Override - public void render(ElementTemplate eleTemplate, Object data, XWPFTemplate template) { - NiceXWPFDocument doc = template.getXWPFDocument(); - RunTemplate runTemplate = (RunTemplate) eleTemplate; - XWPFRun run = runTemplate.getRun(); - String text = run.getText(0); - try { - // w:tbl-w:tr-w:tc-w:p-w:tr - XmlCursor newCursor = ((XWPFParagraph) run.getParent()).getCTP().newCursor(); - newCursor.toParent(); - newCursor.toParent(); - newCursor.toParent(); - XmlObject object = newCursor.getObject(); - XWPFTable table = doc.getAllTable((CTTbl) object); - render(text, table, (List<Map<String, String>>) data, template, text.substring(2).substring(0, text.length() - 4)); - run.setText("", 0); - } catch (Exception e) { - logger.error("dynamic table error:" + e.getMessage(), e); - } - } - - private void render(String text, XWPFTable table, List<Map<String, String>> subListData, XWPFTemplate template, String subTableName) { - List<XWPFTableRow> rows = table.getRows(); - if (rows != null && rows.size() > 0) { - //琛ㄦ牸娓叉煋寮�濮嬭锛堝瓙琛ㄥ瓧娈佃〃杈惧紡鎵�鍦ㄨ锛� - int tableRowIndex = -1; - for (int i = 0; i < rows.size(); i++) { - XWPFTableCell cell = rows.get(i).getCell(0); - if (cell != null && cell.getText().indexOf(text) != -1) { - tableRowIndex = i + 1; - break; - } - } - if (tableRowIndex > -1) { - XWPFTableRow xwpfTableRow = rows.get(tableRowIndex); - List<XWPFTableCell> cells = xwpfTableRow.getTableCells(); - if (cells != null && cells.size() > 0) { - String[] keys = new String[cells.size()]; - for (int i = 0; i < cells.size(); i++) { - XWPFTableCell xwpfTableCell = cells.get(i); - String expression = xwpfTableCell.getText(); - if (!StringUtils.isEmpty(expression.trim())) { - expression = expression.trim(); - keys[i] = parseString(expression, subTableName); - } - } - table.removeRow(tableRowIndex); - if (CollectionUtil.isEmpty(subListData)) { - return; - } - for (int i = 0; i < subListData.size(); i++) { - table.insertNewTableRow(tableRowIndex); - XWPFTableRow currentRow = table.getRow(tableRowIndex); - int cellCount = cells.size() < keys.length ? keys.length : cells.size(); - for (int j = 0; j < cellCount; j++) { - currentRow.addNewTableCell(); - } - setValues(table.getRow(tableRowIndex).getTableCells(), keys, subListData.get(i)); - tableRowIndex++; - } - } - } - } - } - - private void setValues(List<XWPFTableCell> cells, String[] keys, Map subData) { - if (cells != null && keys != null && cells.size() > 0 && subData != null && subData.size() > 0) { - for (int i = 0; i < cells.size(); i++) { - String key = keys[i]; - if (StringUtils.isEmpty(key)) { - continue; - } - Object val = subData.get(key) ; - if (val == null) { - val = ""; - } - cells.get(i).setText(val+""); - } - } - - } - - - /** - * 瑙f瀽瀛楃涓� - */ - public static String parseString(String text, String table) { - Matcher matcher = Pattern.compile("(\\$\\{)(" + makeQueryStringAllRegExp(table) + "\\.+)([\\w]+)(\\})").matcher(text); - StringBuilder sb = new StringBuilder(); - while (matcher.find()) { - System.out.println(matcher.group()); - sb.append(matcher.group(3)); - } - return sb.length() > 0 ? sb.toString() : null; - } - - /** - * 杞箟姝e垯鐗规畩瀛楃 锛�$()*+.[]?\^{} - * \\闇�瑕佺涓�涓浛鎹紝鍚﹀垯replace鏂规硶鏇挎崲鏃朵細鏈夐�昏緫bug - */ - public static String makeQueryStringAllRegExp(String str) { - if (StringUtils.isBlank(str)) { - return str; - } - - return str.replace("\\", "\\\\").replace("*", "\\*") - .replace("+", "\\+").replace("|", "\\|") - .replace("{", "\\{").replace("}", "\\}") - .replace("(", "\\(").replace(")", "\\)") - .replace("^", "\\^").replace("$", "\\$") - .replace("[", "\\[").replace("]", "\\]") - .replace("?", "\\?").replace(",", "\\,") - .replace(".", "\\.").replace("&", "\\&"); - } - - -} diff --git a/src/main/java/com/product/print/service/PrintRealizeService.java b/src/main/java/com/product/print/service/PrintRealizeService.java index 3744f9f..9e6b741 100644 --- a/src/main/java/com/product/print/service/PrintRealizeService.java +++ b/src/main/java/com/product/print/service/PrintRealizeService.java @@ -1,7 +1,11 @@ package com.product.print.service; +import cn.hutool.core.collection.CollectionUtil; +import cn.hutool.core.io.FileUtil; +import cn.hutool.core.net.URLEncodeUtil; import com.deepoove.poi.XWPFTemplate; import com.deepoove.poi.config.Configure; +import com.deepoove.poi.config.ConfigureBuilder; import com.deepoove.poi.data.TextRenderData; import com.deepoove.poi.data.style.Style; import com.product.common.lang.StringUtils; @@ -16,16 +20,14 @@ import com.product.print.config.CmnCode; import com.product.print.config.CmnConst; import com.product.print.service.ide.IPrintRealizeService; +import com.product.print.util.DynamicTableRenderPolicy; import com.product.util.BaseUtil; -import com.product.util.SystemParamReplace; -import com.product.util.config.SystemParamSet; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletResponse; import java.io.*; -import java.net.URLEncoder; import java.util.*; import java.util.stream.Collectors; @@ -40,300 +42,220 @@ public class PrintRealizeService extends AbstractBaseService implements IPrintRealizeService { - @Autowired - FileManagerService fileManagerService; -/* - public static void main(String[] args) { - Map<String, Object> t = new HashMap<>(); - Integer a = 1; - t.put("test", 1); - t.put("test2", true); - t.put("test3", 0.11); - Map<String, String> cc = (Map<String, String>) (Map) t; - System.out.println(cc); - } -*/ -/* - public static void main(String[] args) throws Exception { - Map<String, Object> map = new HashMap<>(); - List<Map<String, String>> subMapList = new ArrayList<>(); - for (int i = 0; i < 5; i++) { - Map<String, String> subMap = new HashMap<>(); - subMap.put("a1", "鍝佸悕鈥斺��" + (i + 1)); - subMap.put("a2", "鍗曚綅鈥斺��" + (i + 1)); - subMap.put("a3", "鏁伴噺鈥斺��" + (i + 1)); - subMap.put("a4", "鐢ㄩ�斺�斺��" + (i + 1)); - subMapList.add(subMap); - } - map.put("BGYPLYB_SUB", subMapList); - HackLoopTableRenderPolicy policy = new HackLoopTableRenderPolicy();//鍒涘缓涓�涓垪琛ㄧ殑瑙勫垯 - Configure config = Configure.newBuilder().customPolicy("BGYPLYB_SUB", policy).customPolicy("test_table", policy).build(); - File targetFile = new File("C:\\Users\\cheng\\Desktop\\replaceWord.docx"); - File file = new File("C:\\Users\\cheng\\Desktop\\鍔炲叕鐢ㄥ搧棰嗙敤琛�.docx"); - if (targetFile.exists()) { - targetFile.delete(); - } - targetFile.createNewFile(); - try (FileOutputStream is = new FileOutputStream(targetFile); - ) { - XWPFTemplate render = XWPFTemplate.compile(file, config).render(map); - render.write(is); - render.close(); - } - } -*/ - - /** - * word鎵撳嵃澶嶉�夋澶勭悊 - * @param fs - */ - public static void dataConvertCheckedData(FieldSetEntity fse) { - - TextRenderData selSymbol = new TextRenderData(CmnConst.PRINT_CHECKED_CHAR, new Style(CmnConst.PRINT_FONT,14)); - TextRenderData unselSymbol = new TextRenderData(CmnConst.PRINT_UNCHECKED_CHAR, new Style(CmnConst.PRINT_FONT,14)); - - //鑾峰彇琛ㄥ崟瀛楁 - Object[] fields = fse.getMeta().getFields(); - if (fields != null) { - for(int i = 0; i < fields.length; ++i) { - - //鑾峰彇淇濆瓨鐨勬暟鎹�� - String dataSaveValue = fse.getString(fields[i] + CmnConst._SAVE_VALUE); - if (!BaseUtil.strIsNull(dataSaveValue)) { - - //鑾峰彇姣忎釜瀛楁鐨刴eta淇℃伅 - FieldSetEntity meta = fse.getMeta().getFieldMeta(fields[i].toString()); - if (meta != null && meta.getString(CmnConst.FIELD_REFERECE) != null && meta.getString(CmnConst.FIELD_REFERECE).indexOf("銆�")>-1) { - - //鏁版嵁瀵瑰簲鍙傜収淇℃伅 - DataTableEntity dictInfos = getMetaAndCacheDictInfo(meta); - if (!BaseUtil.dataTableIsEmpty(dictInfos)) { - for (int j = 0; j < dictInfos.getRows(); j++) { - - //鑾峰彇姣忎釜鍙傜収瀵瑰簲鍊� - String dict_value = dictInfos.getFieldSetEntity(j).getString(CmnConst.DICT_VALUE); - - if (dataSaveValue.indexOf(dict_value)>-1) { - fse.setValue(fields[i]+"_" + dict_value, selSymbol); - }else { - fse.setValue(fields[i]+"_" + dict_value, unselSymbol); - } - } - } - } - } - } - } - } - - /** - * 鑾峰彇瀛楁瀵瑰簲鏁版嵁瀛楀吀淇℃伅 - * @param fieldMate - * @return - */ - public static DataTableEntity getMetaAndCacheDictInfo(FieldSetEntity fieldMate) { - if (fieldMate != null && fieldMate.getString(CmnConst.FIELD_REFERECE) != null) { - int a = fieldMate.getString(CmnConst.FIELD_REFERECE).indexOf("銆�"); - int b = fieldMate.getString(CmnConst.FIELD_REFERECE).indexOf("銆�"); - if (b > 1 && a == 0) { - return DataPoolCacheImpl.getInstance().getCacheData("鏁版嵁瀛楀吀閰嶇疆淇℃伅", new String[]{fieldMate.getString(CmnConst.FIELD_REFERECE).substring(a + 1, b)}); - } - } - return null; - } - - - @Override - public void printWord(FieldSetEntity fse, HttpServletResponse response) throws BaseException { - // 鎵撳嵃閰嶇疆uuid - String print_uuid = fse.getString("~" + CmnConst.PRINT_TEMP + "~"); - //鏌ヨ鎵撳嵃閰嶇疆 - FieldSetEntity fieldSetEntity = getBaseDao().getFieldSetEntity(CmnConst.TABLE_PRINT_CONFIG, print_uuid, false); - if (fieldSetEntity == null || StringUtils.isEmpty(fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE))) { - throw new BaseException(CmnCode.PRINT_CONFIG_NOT_EXIST.getValue(), CmnCode.PRINT_CONFIG_NOT_EXIST.getText()); - } - // 鎵撳嵃妯℃澘闄勪欢uuid - String template_uuid = fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE); - // 鑾峰彇鎵撳嵃妯℃澘 - File file = getFile(template_uuid); - // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭�� - getBaseDao().loadPromptData(fse); - // 鏂囦欢鍚嶅墠缂�閮ㄥ垎 - Object tempKey = UUID.randomUUID(); - // 鏇挎崲鍚庣殑word涓存椂璺緞 - String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx"; - File wordTemp = new File(localTempPathWord); - try { - new File(Global.getSystemConfig("temp.dir", "")).mkdirs(); - wordTemp.createNewFile(); - } catch (Exception e) { - e.printStackTrace(); - throw new BaseException(CmnCode.PRINT_CONTENT_FAIL.getValue(), CmnCode.PRINT_CONTENT_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); - } - //灏嗘ā鍜屽綋鍓峟se鐨剉alues鏀惧叆杩涜鏇挎崲 - try (FileOutputStream is = new FileOutputStream(wordTemp); - OutputStream out = response.getOutputStream()) { - Map<String, DataTableEntity> subData = fse.getSubData(); + @Autowired + FileManagerService fileManagerService; - Configure.ConfigureBuilder configureBuilder = null; - if (subData != null && subData.size() > 0) { - for (Map.Entry<String, DataTableEntity> vv : - subData.entrySet()) { - getBaseDao().loadPromptData(vv.getValue()); - if (!DataTableEntity.isEmpty(vv.getValue())) { - List<Map> collect = vv.getValue().getData().stream().map(item -> (Map<String, String>) ((Map) item.getValues())).collect(Collectors.toList()); - fse.setValue(vv.getKey(), collect); - } else { - fse.setValue(vv.getKey(), new ArrayList<>()); - } - if (configureBuilder == null) { - configureBuilder = Configure.newBuilder(); - } - configureBuilder.customPolicy(vv.getKey(), new HackLoopTableRenderPolicy()); - } - } - - //澶嶉�夋澶勭悊 - dataConvertCheckedData(fse); - - XWPFTemplate render = XWPFTemplate.compile(file, configureBuilder == null ? Configure.createDefault() : configureBuilder.build()).render(fse.getValues()); - render.write(is); - render.close(); - response.setContentType("multipart/form-data"); - response.setHeader("Access-Control-Allow-Origin", "*"); // 鍏佽鎵�鏈� - //璁剧疆鍝嶅簲 - response.setContentType("application/octet-stream;charset=UTF-8"); - // 灏嗗搷搴斿ご涓殑Content-Disposition鏆撮湶鍑烘潵锛屼笉鐒跺墠绔幏鍙栦笉鍒� - response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); - - String fileName = null; - if(!BaseUtil.strIsNull(fieldSetEntity.getString(CmnConst.PRINT_FILE_NAME))) { - fileName = SystemParamReplace.replaceParams(fieldSetEntity.getString(CmnConst.PRINT_FILE_NAME), fse); - }else { - fileName = fieldSetEntity.getString(CmnConst.PRINT_NAME); + /** + * word鎵撳嵃澶嶉�夋澶勭悊 + * + * @param fse + */ + public static void dataConvertCheckedData(FieldSetEntity fse) { + + TextRenderData selSymbol = new TextRenderData(CmnConst.PRINT_CHECKED_CHAR, new Style(CmnConst.PRINT_FONT, 14)); + TextRenderData unselSymbol = new TextRenderData(CmnConst.PRINT_UNCHECKED_CHAR, new Style(CmnConst.PRINT_FONT, 14)); + + //鑾峰彇琛ㄥ崟瀛楁 + Object[] fields = fse.getMeta().getFields(); + if (fields == null) { + return; + } + for (int i = 0; i < fields.length; ++i) { + //鑾峰彇淇濆瓨鐨勬暟鎹�� + String dataSaveValue = fse.getString(fields[i] + CmnConst._SAVE_VALUE); + if (BaseUtil.strIsNull(dataSaveValue)) { + continue; } - fileName+=".docx"; - - // 鍦ㄥ搷搴斿ご涓殑Content-Disposition閲岃缃枃浠跺悕绉� - response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); - InputStream isPdf = new FileInputStream(wordTemp); - int len; - byte[] b = new byte[1024]; - // 杈撳嚭 - while ((len = isPdf.read(b)) > 0) { - out.write(b, 0, len); - } - isPdf.close(); - } catch (Exception e) { - e.printStackTrace(); - throw new BaseException(CmnCode.PRINT_CONTENT_FAIL.getValue(), CmnCode.PRINT_CONTENT_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); - } finally { - //鍒犻櫎word涓存椂鏂囦欢 - if (wordTemp.exists()) { - System.out.println(wordTemp.getPath()); -// wordTemp.delete(); - } - if (file != null) { - file.delete(); - } - } + //鑾峰彇姣忎釜瀛楁鐨刴eta淇℃伅 + FieldSetEntity meta = fse.getMeta().getFieldMeta(fields[i].toString()); + if (meta == null || meta.getString(CmnConst.FIELD_REFERECE) == null || meta.getString(CmnConst.FIELD_REFERECE).indexOf("銆�") == -1) { + continue; + } + //鏁版嵁瀵瑰簲鍙傜収淇℃伅 + DataTableEntity dictInfos = getMetaAndCacheDictInfo(meta); + if (BaseUtil.dataTableIsEmpty(dictInfos)) { + continue; + } + for (int j = 0; j < dictInfos.getRows(); j++) { + //鑾峰彇姣忎釜鍙傜収瀵瑰簲鍊� + String dict_value = dictInfos.getFieldSetEntity(j).getString(CmnConst.DICT_VALUE); + if (dataSaveValue.indexOf(dict_value) > -1) { + fse.setValue(fields[i] + "_" + dict_value, selSymbol); + continue; + } + fse.setValue(fields[i] + "_" + dict_value, unselSymbol); + } + } + } - } - - - @Override - public void print(FieldSetEntity fse, HttpServletResponse response) throws BaseException { - // 鎵撳嵃閰嶇疆uuid - String print_uuid = fse.getString("~" + CmnConst.PRINT_TEMP + "~"); - //鏌ヨ鎵撳嵃閰嶇疆 - FieldSetEntity fieldSetEntity = getBaseDao().getFieldSetEntity(CmnConst.TABLE_PRINT_CONFIG, print_uuid, false); - if (fieldSetEntity == null || StringUtils.isEmpty(fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE))) { - throw new BaseException(CmnCode.PRINT_CONFIG_NOT_EXIST.getValue(), CmnCode.PRINT_CONFIG_NOT_EXIST.getText()); - } - // 鎵撳嵃妯℃澘闄勪欢uuid - String template_uuid = fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE); - // 鑾峰彇鎵撳嵃妯℃澘 - File file = getFile(template_uuid); - // 鍔犺浇鍙傜収 鎵撳嵃鏃朵娇鐢ㄦ樉绀哄�� 鑰屼笉鏄疄闄呭�� - getBaseDao().loadPromptData(fse); - // 鏂囦欢鍚嶅墠缂�閮ㄥ垎 - Object tempKey = UUID.randomUUID(); - // 鏇挎崲鍚庣殑word涓存椂璺緞 - String localTempPathWord = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".docx"; - // 杞崲pdf鐨勪复鏃惰矾寰� - String localTempPathPdf = Global.getSystemConfig("temp.dir", "") + File.separator + "temp_print_" + tempKey + ".pdf"; - File wordTemp = new File(localTempPathWord); - File pdfTemp = null; - try { - new File(Global.getSystemConfig("temp.dir", "")).mkdirs(); - wordTemp.createNewFile(); - } catch (Exception e) { - e.printStackTrace(); - throw new BaseException(CmnCode.PRINT_CONTENT_FAIL.getValue(), CmnCode.PRINT_CONTENT_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); - } - //灏嗘ā鍜屽綋鍓峟se鐨剉alues鏀惧叆杩涜鏇挎崲 - try (FileOutputStream is = new FileOutputStream(wordTemp); - OutputStream out = response.getOutputStream()) { - Map<String, DataTableEntity> subData = fse.getSubData(); + /** + * 鑾峰彇瀛楁瀵瑰簲鏁版嵁瀛楀吀淇℃伅 + * + * @param fieldMate + * @return + */ + public static DataTableEntity getMetaAndCacheDictInfo(FieldSetEntity fieldMate) { + if (fieldMate != null && fieldMate.getString(CmnConst.FIELD_REFERECE) != null) { + int a = fieldMate.getString(CmnConst.FIELD_REFERECE).indexOf("銆�"); + int b = fieldMate.getString(CmnConst.FIELD_REFERECE).indexOf("銆�"); + if (b > 1 && a == 0) { + return DataPoolCacheImpl.getInstance().getCacheData("鏁版嵁瀛楀吀閰嶇疆淇℃伅", new String[]{fieldMate.getString(CmnConst.FIELD_REFERECE).substring(a + 1, b)}); + } + } + return null; + } - Configure.ConfigureBuilder configureBuilder = null; - if (subData != null && subData.size() > 0) { - for (Map.Entry<String, DataTableEntity> vv : - subData.entrySet()) { - getBaseDao().loadPromptData(vv.getValue()); - if (!DataTableEntity.isEmpty(vv.getValue())) { - List<Map> collect = vv.getValue().getData().stream().map(item -> (Map<String, String>) ((Map) item.getValues())).collect(Collectors.toList()); - fse.setValue(vv.getKey(), collect); - } else { - fse.setValue(vv.getKey(), new ArrayList<>()); - } - if (configureBuilder == null) { - configureBuilder = Configure.newBuilder(); - } - configureBuilder.customPolicy(vv.getKey(), new HackLoopTableRenderPolicy()); - } - } - XWPFTemplate render = XWPFTemplate.compile(file, configureBuilder == null ? Configure.createDefault() : configureBuilder.build()).render(fse.getValues()); - render.write(is); - render.close(); - response.setContentType("multipart/form-data"); - response.setHeader("Content-Disposition", "attachment;"); - // 浣跨敤openOffice 杞崲涓簆df - pdfTemp = PdfConcurrenceUtil.convertToPdf(wordTemp.getPath(), localTempPathPdf,"docx"); - InputStream isPdf = new FileInputStream(pdfTemp); - int len; - byte[] b = new byte[1024]; - // 杈撳嚭 - while ((len = isPdf.read(b)) > 0) { - out.write(b, 0, len); - } - isPdf.close(); - } catch (Exception e) { - e.printStackTrace(); - throw new BaseException(CmnCode.PRINT_CONTENT_FAIL.getValue(), CmnCode.PRINT_CONTENT_FAIL.getText() + (e.getMessage() != null ? e.getMessage() : "")); - } finally { - // 鍒犻櫎涓存椂鏂囦欢 - if (pdfTemp != null && pdfTemp.exists()) { - pdfTemp.delete(); - } - if (wordTemp.exists()) { - wordTemp.delete(); - } - if (file != null) { - file.delete(); - } - } + /** + * 鎵撳嵃浼犺緭pdf娴佸埌鍓嶇 + * + * @param fse 鎵撳嵃鏁版嵁 + * @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 + "~")); + //鑾峰彇鍒版浛鎹㈠悗鐨勬枃浠惰矾寰� 锛坧df鏂囦欢鎴栬�厀ord鏂囦欢锛� + String tempPdfFilePath = replaceTemplateFileOut(printConfig, fse, isConvertPdf); + //鑾峰彇鏂囦欢鍚� + String fileName = BaseUtil.ifNull(printConfig.getString(CmnConst.PRINT_FILE_NAME), printConfig.getString(CmnConst.PRINT_NAME)); + if (isConvertPdf) { + fileName = fileName + ".pdf"; + } else { + fileName = fileName + ".docx"; + } + //璁剧疆鍝嶅簲 + response.setContentType("application/octet-stream;charset=UTF-8"); + // 灏嗗搷搴斿ご涓殑Content-Disposition鏆撮湶鍑烘潵锛屼笉鐒跺墠绔幏鍙栦笉鍒� + response.setHeader("Access-Control-Expose-Headers", "Content-Disposition"); + // 璁剧疆鑷畾涔夊ご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) { + e.printStackTrace(); + throw new BaseException(CmnCode.PRINT_CONTENT_FAIL, e); + } finally { + //鍒犻櫎pdf涓存椂鏂囦欢 + FileUtil.del(tempPdfFilePath); + } - private File getFile(String template_uuid) throws BaseException { - try { - return fileManagerService.getFile(template_uuid); - } catch (BaseException e) { - e.printStackTrace(); - //鑾峰彇鎵撳嵃妯℃澘閿欒 - throw new BaseException(CmnCode.GET_PRINT_TEMPLATE_FILE_FAIL.getValue(), CmnCode.GET_PRINT_TEMPLATE_FILE_FAIL.getText()); - } - } + } + + /** + * 鑾峰彇鎵撳嵃閰嶇疆 + * + * @param uuid 鎵撳嵃閰嶇疆uuid + * @return + * @throws BaseException + */ + public FieldSetEntity getPrintConfig(String uuid) throws BaseException { + //鏌ヨ鎵撳嵃閰嶇疆 + FieldSetEntity fieldSetEntity = getBaseDao().getFieldSetEntity(CmnConst.TABLE_PRINT_CONFIG, uuid, false); + if (fieldSetEntity == null || StringUtils.isEmpty(fieldSetEntity.getString(CmnConst.PRINT_TEMPLATE))) { + throw new BaseException(CmnCode.PRINT_CONFIG_NOT_EXIST.getValue(), CmnCode.PRINT_CONFIG_NOT_EXIST.getText()); + } + return fieldSetEntity; + } + + /** + * 鏇挎崲妯℃澘鏂囦欢骞惰緭鍑� + * + * @param printConf 鎵撳嵃閰嶇疆 + * @param fse 鏇挎崲鏁版嵁 + * @param isConvertPdf 鏄惁杞崲涓簆df + * @return 鏇挎崲鍚庣殑鏂囦欢璺緞 + * @throws BaseException 寮傚父 + */ + private String replaceTemplateFileOut(FieldSetEntity printConf, FieldSetEntity fse, boolean isConvertPdf) throws BaseException { + // 鎵撳嵃妯℃澘闄勪欢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"; + replaceWord(localTempPathWord, file.getPath(), fse); + file.delete(); + 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; + } + + /** + * 鏇挎崲word妯℃澘 + * + * @param outPath 杈撳嚭璺緞 + * @param templatePath 妯℃澘璺緞 + * @param dataFse 鏇挎崲鏁版嵁 + */ + public static void replaceWord(String outPath, String templatePath, FieldSetEntity dataFse) { + //杞崲鏁版嵁涓哄閫夋 + dataConvertCheckedData(dataFse); + //鍏嬮殕涓�浠絭alues + Map<String, Object> cloneValues = new HashMap(dataFse.getValues()); + //鑾峰彇瀛愯〃鏁版嵁 + Map<String, DataTableEntity> subDataMap = dataFse.getSubData(); + ConfigureBuilder config = Configure.createDefault().builder(); + if (!CollectionUtil.isEmpty(subDataMap)) { + for (Map.Entry<String, DataTableEntity> entry : subDataMap.entrySet()) { + cloneValues.put(entry.getKey(), entry.getValue().getData().stream().map(item -> (Map<String, Object>) ((Map) item.getValues())).collect(Collectors.toList())); + config.bind(entry.getKey(), new DynamicTableRenderPolicy(entry.getKey())); + } + } + try { + //妫�鏌ヨ緭鍑烘枃浠舵槸鍚﹀瓨鍦紝涓嶅瓨鍦ㄥ垯鍒涘缓 + FileUtil.touch(outPath); + System.out.println(cloneValues); + XWPFTemplate.compile(templatePath, config.build()).render(cloneValues).writeToFile(outPath); + } catch (Exception e) { + e.printStackTrace(); + FileUtil.del(outPath); + throw new BaseException(CmnCode.REPLACE_TEMPLATE_CONTENT_ERROR, e); + } + } + + /** + * 鑾峰彇妯℃澘闄勪欢 + * + * @param templateUid 妯℃澘闄勪欢uuid + * @return + * @throws BaseException + */ + private File getTemplateFile(String templateUid) throws BaseException { + try { + return fileManagerService.getFile(templateUid); + } catch (BaseException e) { + e.printStackTrace(); + //鑾峰彇鎵撳嵃妯℃澘閿欒 + throw new BaseException(CmnCode.GET_PRINT_TEMPLATE_FILE_FAIL.getValue(), CmnCode.GET_PRINT_TEMPLATE_FILE_FAIL.getText()); + } + } } 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 ccea719..3b5b4d2 100644 --- a/src/main/java/com/product/print/service/ide/IPrintRealizeService.java +++ b/src/main/java/com/product/print/service/ide/IPrintRealizeService.java @@ -14,8 +14,13 @@ */ public interface IPrintRealizeService { - void printWord(FieldSetEntity fse, HttpServletResponse response) throws BaseException; - - void print(FieldSetEntity fse, HttpServletResponse response) throws BaseException; + /** + * 鎵撳嵃 + * @param fse 鎵撳嵃鏁版嵁 + * @param response 鍝嶅簲 + * @param isConvertPdf 鏄惁杞崲涓簆df + * @throws BaseException + */ + void print(FieldSetEntity fse, HttpServletResponse response,boolean isConvertPdf) throws BaseException; } diff --git a/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java new file mode 100644 index 0000000..9590e86 --- /dev/null +++ b/src/main/java/com/product/print/util/DynamicTableRenderPolicy.java @@ -0,0 +1,177 @@ +package com.product.print.util; + +import com.deepoove.poi.exception.RenderException; +import com.deepoove.poi.render.RenderContext; +import com.deepoove.poi.template.run.RunTemplate; +import com.deepoove.poi.util.TableTools; +import com.product.common.lang.StringUtils; +import com.product.core.exception.BaseException; +import com.product.print.config.CmnCode; +import org.apache.poi.xwpf.usermodel.*; + +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +/** + * @Author cheng + * @Date 2023/6/27 15:50 + * @Desc + */ +public class DynamicTableRenderPolicy extends com.deepoove.poi.policy.DynamicTableRenderPolicy { + + private String replaceKey; + + private final String indexKey = "~index~"; + + public DynamicTableRenderPolicy(String replaceKey) { + this.replaceKey = replaceKey; + + } + + private XWPFRun run; + + @Override + public void doRender(RenderContext<Object> context) throws Exception { + RunTemplate runTemplate = (RunTemplate) context.getEleTemplate(); + XWPFRun run = runTemplate.getRun(); + try { + if (!TableTools.isInsideTable(run)) { + throw new IllegalStateException( + "The template tag " + runTemplate.getSource() + " must be inside a table"); + } + XWPFTableCell cell = (XWPFTableCell) ((XWPFParagraph) run.getParent()).getBody(); + XWPFTable table = cell.getTableRow().getTable(); + render(table, context.getData()); + } catch (Exception e) { + throw new RenderException("Dynamic render table error:" + e.getMessage(), e); + } + } + + @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涓殑鍐呭 + //鑾峰彇鏈�鍚庝竴琛� + XWPFTableRow xwpfTableRow = rows.get(rows.size() - 1); + String tableExpression = "{{" + this.replaceKey + "}}"; + //鑾峰彇琛ㄦ牸璧峰琛屽拰缁撴潫琛� + int startRowIndex = -1; + int endRowIndex = -1; + rows: + for (int i = 0; i < rows.size(); i++) { + XWPFTableRow row = rows.get(i); + for (int j = 0; j < row.getTableCells().size(); j++) { + if (startRowIndex > -1 && endRowIndex > -1) { + break rows; + } + XWPFTableCell cell = row.getTableCells().get(j); + String text = cell.getText(); + if (tableExpression.equals(text)) { + startRowIndex = i; + } + if ("{{$~end~}}".equals(text)) { + endRowIndex = i; + } + + } + } + //琛ㄦ牸璧峰琛屼笉鍦ㄧ涓�琛屼笖娌℃湁鎵惧埌缁撴潫鏍囪 + if (startRowIndex > 0 && endRowIndex == -1) { + throw new BaseException(CmnCode.NOT_FIND_END_FLAG); + } + + if (startRowIndex == 0 && endRowIndex > 0) { + //鍒犻櫎缁撴潫鏍囪鎵�鍦ㄨ + xwpfTable.removeRow(endRowIndex); + } + + //璇诲彇鏈�鍚庤姣忎釜鍗曞厓鏍肩殑鍊硷紝璋冪敤getReplaceKey鏂规硶鑾峰彇琛ㄨ揪寮忎腑鐨勫�� + String[] fieldNames = new String[xwpfTableRow.getTableCells().size()]; +// for (int i = 0; i < xwpfTableRow.getTableCells().size(); i++) { +// XWPFTableCell cell = xwpfTableRow.getTableCells().get(i); +// String text = cell.getText(); +// String replaceKey = getReplaceKey(text); +// //鑾峰彇琛ㄨ揪寮忎腑鐨勫�� +// if (StringUtils.isEmpty(replaceKey)) { +// //璁剧疆鍗曞厓鏍间负绌哄�� +// replaceKey = ""; +// } +// fieldNames[i] = replaceKey; +// } +// //璇诲彇瀹屾瘯鍚庡垹闄ゆ渶鍚庝竴琛� +// xwpfTable.removeRow(rows.size() - 1); + //鑾峰彇鎵�鏈夌殑琛屽垽鏂崟鍏冩牸鏄惁鏈夊�兼病鏈夊氨鍒犻櫎璇ヨ +// List<XWPFTableRow> tableRows = xwpfTable.getRows(); +// List<XWPFTableRow> deleteRows = new ArrayList<>(); +// for (int i = 0; i < tableRows.size(); i++) { +// XWPFTableRow row = tableRows.get(i); +// boolean flag = false; +// for (int j = 0; j < row.getTableCells().size(); j++) { +// XWPFTableCell cell = row.getTableCells().get(j); +// if (StringUtils.isNotEmpty(cell.getText())) { +// flag = true; +// break; +// } +// } +// if (!flag) { +// deleteRows.add(row); +// } +// } +// //閬嶅巻瑕佸垹闄ょ殑琛岃幏鍙栨墍鍦ㄧ殑涓嬫爣杩涜鍒犻櫎 +// for (int i = 0; i < deleteRows.size(); i++) { +// XWPFTableRow row = deleteRows.get(i); +// int index = xwpfTable.getRows().indexOf(row); +// xwpfTable.removeRow(index); +// } +// //閬嶅巻鏁版嵁闆嗗悎锛屾瘡涓猰ap瀵瑰簲涓�琛屾暟鎹� +// for (int i = 0; i < subTableData.size(); i++) { +// Map<String, Object> map = subTableData.get(i); +// //鍒涘缓涓�琛� +// XWPFTableRow row = xwpfTable.createRow(); +// //閬嶅巻瀛楁姣忎釜瀛楁鍒涘缓涓�涓崟鍏冩牸 +// for (int j = 0; j < fieldNames.length; j++) { +// //褰撳墠鍗曞厓鏍� +// XWPFTableCell cell; +// //鍒ゆ柇row涓j涓崟鍏冩牸鏄惁瀛樺湪 +// if (row.getTableCells().size() > j) { +// cell = row.getTableCells().get(j); +// } else { +// cell = row.createCell(); +// } +// //璁剧疆鍗曞厓鏍肩殑鍊间粠map涓彇鍑� +// //鍒ゆ柇鏄惁鏄簭鍙峰垪 +// if (indexKey.equals(fieldNames[j])) { +// cell.setText(String.valueOf(i + 1)); +// continue; +// } +// Object value = map.get(fieldNames[j]); +// if (value == null) { +// value = ""; +// } +// //璁剧疆鍗曞厓鏍肩殑鍊� +// cell.setText(value.toString()); +// } +// } + + } + + /** + * 瑙f瀽琛ㄨ揪寮忎腑鐨勫�艰幏鍙� 浠{$寮�澶达紝浠}缁撳熬鐨勫瓧绗︿覆鍙栧嚭$鍚庨潰鐨勫�兼埅姝㈠埌}} + */ + private String getReplaceKey(String text) { + //浣跨敤姝e垯瑙f瀽琛ㄨ揪寮忎腑鐨勫�艰幏鍙� 浠{$寮�澶达紝浠}缁撳熬鐨勫瓧绗︿覆鍙栧嚭$鍚庨潰鐨勫�兼埅姝㈠埌}} + Pattern pattern = Pattern.compile("\\{\\{\\$(.*?)\\}\\}"); + Matcher matcher = pattern.matcher(text); + if (matcher.find()) { + return matcher.group(1); + } + return null; + } +} -- Gitblit v1.9.2