ソースを参照

测试word替换的问题

huZhiHao 5 年 前
コミット
48dc6599c8

+ 69 - 45
src/main/java/platform/common/util/BokeWordUtils.java

@@ -22,14 +22,15 @@ public class BokeWordUtils {
 
     /**
      * 构造函数
+     *
      * @param inputUrl
      * @param tableList
      */
-    public BokeWordUtils (String inputUrl, List<String[]> tableList){
+    public BokeWordUtils(String inputUrl, List<String[]> tableList) {
         this(inputUrl, new HashMap<>(), tableList);
     }
 
-    public BokeWordUtils (String inputUrl, Map<String, String> textMap, List<String[]> tableList){
+    public BokeWordUtils(String inputUrl, Map<String, String> textMap, List<String[]> tableList) {
 
         try {
             System.out.println("===============开始读取word模版===================");
@@ -49,9 +50,10 @@ public class BokeWordUtils {
     /**
      * 根据模板生成新word文档
      * 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
-     * @param inputUrl 模板存放地址
+     *
+     * @param inputUrl  模板存放地址
      * @param outputUrl 新文档存放地址
-     * @param textMap 需要替换的信息集合
+     * @param textMap   需要替换的信息集合
      * @param tableList 需要插入的表格信息集合
      */
     public static boolean changWord(String inputUrl, String outputUrl,
@@ -85,9 +87,10 @@ public class BokeWordUtils {
      * 判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
      * author:hzh
      * description:改为下载
-     * @param inputUrl 模板存放地址
-     * @param response 新文档存放地址
-     * @param textMap 需要替换的信息集合
+     *
+     * @param inputUrl  模板存放地址
+     * @param response  新文档存放地址
+     * @param textMap   需要替换的信息集合
      * @param tableList 需要插入的表格信息集合
      */
     public static boolean changWord(String inputUrl, HttpServletResponse response,
@@ -119,43 +122,46 @@ public class BokeWordUtils {
 
     /**
      * 输出到客户端
+     *
      * @param fileName 输出文件名
      */
-    public BokeWordUtils write(HttpServletResponse response, String fileName) throws IOException{
+    public BokeWordUtils write(HttpServletResponse response, String fileName) throws IOException {
         response.reset();
         response.setContentType("application/octet-stream; charset=utf-8");
-        response.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode(fileName, "UTF-8"));
+        response.setHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
         write(response.getOutputStream());
         return this;
     }
 
     /**
      * 输出数据流
+     *
      * @param os 输出数据流
      */
-    public BokeWordUtils write(OutputStream os) throws IOException{
+    public BokeWordUtils write(OutputStream os) throws IOException {
         document.write(os);
         return this;
     }
 
     /**
      * 替换段落文本
+     *
      * @param document docx解析对象
-     * @param textMap 需要替换的信息集合
+     * @param textMap  需要替换的信息集合
      */
-    public static void changeText(XWPFDocument document, Map<String, String> textMap){
+    public static void changeText(XWPFDocument document, Map<String, String> textMap) {
         //获取段落集合
         List<XWPFParagraph> paragraphs = document.getParagraphs();
         for (XWPFParagraph paragraph : paragraphs) {
             //判断此段落时候需要进行替换
             String text = paragraph.getText();
-            if(checkText(text)){
+            if (checkText(text)) {
                 List<XWPFRun> runs = paragraph.getRuns();
                 for (XWPFRun run : runs) {
                     //替换模板原来位置
                     // run.setText(changeValue(run.toString(), textMap),0);
                     String textValue = changeValue(run.toString(), textMap);
-                    run.setText(textValue,0);
+                    run.setText(textValue, 0);
                 }
             }
         }
@@ -163,23 +169,24 @@ public class BokeWordUtils {
 
     /**
      * 替换表格对象方法
-     * @param document docx解析对象
-     * @param textMap 需要替换的信息集合
+     *
+     * @param document  docx解析对象
+     * @param textMap   需要替换的信息集合
      * @param tableList 需要插入的表格信息集合
      */
     public static void changeTable(XWPFDocument document, Map<String, String> textMap,
-                                   List<String[]> tableList){
+                                   List<String[]> tableList) {
         //获取表格对象集合
         List<XWPFTable> tables = document.getTables();
         for (int i = 0; i < tables.size(); i++) {
             //只处理行数大于等于2的表格,且不循环表头
             XWPFTable table = tables.get(i);
             //判断表格是需要替换还是需要插入,判断逻辑有$为替换,表格无$为插入
-            if(checkText(table.getText())){
+            if (checkText(table.getText())) {
                 List<XWPFTableRow> rows = table.getRows();
                 //遍历表格,并替换模板
                 eachTable(rows, textMap);
-            }else{
+            } else {
                 insertTable(table, tableList);
             }
         }
@@ -188,20 +195,21 @@ public class BokeWordUtils {
 
     /**
      * 遍历表格
-     * @param rows 表格行对象
+     *
+     * @param rows    表格行对象
      * @param textMap 需要替换的信息集合
      */
-    public static void eachTable(List<XWPFTableRow> rows ,Map<String, String> textMap){
+    public static void eachTable(List<XWPFTableRow> rows, Map<String, String> textMap) {
         for (XWPFTableRow row : rows) {
             List<XWPFTableCell> cells = row.getTableCells();
             for (XWPFTableCell cell : cells) {
                 //判断单元格是否需要替换
-                if(checkText(cell.getText())){
+                if (checkText(cell.getText())) {
                     List<XWPFParagraph> paragraphs = cell.getParagraphs();
                     for (XWPFParagraph paragraph : paragraphs) {
                         List<XWPFRun> runs = paragraph.getRuns();
                         for (XWPFRun run : runs) {
-                            run.setText(changeValue(run.toString(), textMap),0);
+                            run.setText(changeValue(run.toString(), textMap), 0);
                         }
                     }
                 }
@@ -211,15 +219,16 @@ public class BokeWordUtils {
 
     /**
      * 为表格插入数据,行数不够添加新行
-     * @param table 需要插入数据的表格
+     *
+     * @param table     需要插入数据的表格
      * @param tableList 插入数据集合
      */
-    public static void insertTable(XWPFTable table, List<String[]> tableList){
+    public static void insertTable(XWPFTable table, List<String[]> tableList) {
         //创建行,根据需要插入的数据添加新行,不处理表头
-        for(int i = 0; i < tableList.size(); i++){
-            XWPFTableRow row =table.createRow();
+        for (int i = 0; i < tableList.size(); i++) {
+            XWPFTableRow row = table.createRow();
             List<XWPFTableCell> cells = row.getTableCells();
-            for(int j = 0; j < cells.size(); j++){
+            for (int j = 0; j < cells.size(); j++) {
                 XWPFTableCell cell = cells.get(j);
                 cell.setText(tableList.get(i)[j]);
             }
@@ -236,37 +245,52 @@ public class BokeWordUtils {
 //            }
 //        }
     }
+
     /**
      * 判断文本中时候包含$
+     *
      * @param text 文本
-     * @return 包含返回true,不包含返回false
+     * @return 包含返回true, 不包含返回false
      */
-    public static boolean checkText(String text){
-        boolean check  =  false;
-        if(text.indexOf("$")!= -1){
+    public static boolean checkText(String text) {
+        boolean check = false;
+        if (text.indexOf("$") != -1) {
             check = true;
         }
         return check;
     }
+
     /**
      * 匹配传入信息集合与模板
-     * @param value 模板需要替换的区域
+     *
+     * @param value   模板需要替换的区域
      * @param textMap 传入信息集合
      * @return 模板需要替换区域信息集合对应值
      */
-    public static String changeValue(String value, Map<String, String> textMap){
+    public static String changeValue(String value, Map<String, String> textMap) {
         Set<Map.Entry<String, String>> textSets = textMap.entrySet();
         for (Map.Entry<String, String> textSet : textSets) {
             //匹配模板与替换值 格式${key}
-            String key = "${"+textSet.getKey()+"}";
-            if(value.indexOf(key)!= -1){
+            String key = "${" + textSet.getKey() + "}";
+            if (value.indexOf(key) != -1) {
                 // value = textSet.getValue();//全部参数替换
-                if(value != null && textSet != null)
+                System.out.println("===================================================================");
+                System.out.println("value:" + value);
+                System.out.println("textSet:" + textSet.toString());
+                System.out.println("===================================================================");
+                if (value != null && textSet != null && textSet.getValue() != null) {
                     value = value.replace(key, textSet.getValue());//仅替换参数
+                }
+                if (value == null || textSet == null || textSet.getValue() == null) {
+                    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+                    System.out.println("value:" + value);
+                    System.out.println("textSet:" + textSet.toString());
+                    System.out.println("!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
+                }
             }
         }
         //模板未匹配到区域替换为空
-        if(checkText(value)){
+        if (checkText(value)) {
             value = "";
         }
         return value;
@@ -292,13 +316,13 @@ public class BokeWordUtils {
         testMap.put("date", DateUtil.getTimeString(new Date()));
 
         List<String[]> tableList = new ArrayList<>();
-        tableList.add(new String[]{"1","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
-        tableList.add(new String[]{"2","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
-        tableList.add(new String[]{"3","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
-        tableList.add(new String[]{"4","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
-        tableList.add(new String[]{"5","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
-        tableList.add(new String[]{"6","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
-        tableList.add(new String[]{"7","1mm","1开开","1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"1", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"2", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"3", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"4", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"5", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"6", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
+        tableList.add(new String[]{"7", "1mm", "1开开", "1uu", "5", "6", "7", "8", "9"});
 //        tableList.add(new String[]{"2","2密码","2B","基金C"});
 //        tableList.add(new String[]{"3","3看看","3B","基看C"});
 //        tableList.add(new String[]{"4","4累了","4B","4谁说的"});

+ 11 - 0
src/main/java/platform/modules/company/service/ProjectApplicationService.java

@@ -961,6 +961,7 @@ public class ProjectApplicationService extends BaseService<ProjectApplication> {
      */
     private FileDown annexThree(ProjectApplication apply) {
         final String annexName = "苏州高新区工业经济发展专项扶持资金申报表";
+        String date = DateUtil.getCurrentDateString("yyyy年MM月dd日");
 
 //        String annexTempUrl = "/Users/xikaiwen/Downloads/annex3.docx";
         String annexTempUrl = repositoryPath + "/docs/annex_template/annex3.docx";
@@ -989,11 +990,21 @@ public class ProjectApplicationService extends BaseService<ProjectApplication> {
         dataMap.put("cn", currencyUnit(companyInfo.getCurrency_unit()));
         dataMap.put("faunit", currencyUnit(companyInfo.getFixed_assets_currency_unit()));
         dataMap.put("taunit", currencyUnit(companyInfo.getTotal_assets_currency_unit()));
+        //填报日期
+        dataMap.put("date", date);
 
         currencyUnit(companyInfo.getFixed_assets_currency_unit());
 
         //近三年营收情况
         for (int i = 0; i < 3; i++) {
+            SimpleDateFormat format = new SimpleDateFormat("yyyy");
+            Calendar c = Calendar.getInstance();
+            c.setTime(new Date());
+            c.add(Calendar.YEAR, 0 - (i + 1));
+            Date y = c.getTime();
+            String year = format.format(y);
+
+            dataMap.put("year" + i, year);
             dataMap.put("oi" + i, operationsInfo.get(i).getOperation_income());
             dataMap.put("si" + i, operationsInfo.get(i).getSales_income());
             dataMap.put("pi" + i, operationsInfo.get(i).getProcessing_income());