Browse Source

试车标准库导出报告样式调整

zhoudazhuang 7 months ago
parent
commit
e1f5c23a89

+ 1 - 1
business-common/src/main/java/com/rongwei/bscommon/sys/service/ZhcxCommissionCheckService.java

@@ -54,7 +54,7 @@ public interface ZhcxCommissionCheckService extends IService<ZhcxCommissionCheck
      * @param response
      * @return
      */
-    int wordExportStandard(Map<String, Object> map, HttpServletResponse response) throws Exception;
+    R wordExportStandard(Map<String, Object> map, HttpServletResponse response) throws Exception;
 
     /**
      * 导入明细

+ 96 - 72
business-common/src/main/java/com/rongwei/bscommon/sys/service/impl/ZhcxCommissionCheckServiceImpl.java

@@ -42,7 +42,6 @@ import lombok.extern.slf4j.Slf4j;
 import org.springframework.beans.BeanUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.core.io.ClassPathResource;
-import org.springframework.scheduling.annotation.Async;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
 import com.rongwei.rwcommoncomponent.excel.aspose.ExcelExportUtil;
@@ -118,32 +117,6 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
         commonFeginClient = autoCommonFeginClient;
     }
 
-    /**
-     * 大类
-     */
-    public static final String GENERA_TITLE = "%s、%s";
-    /**
-     * 小类
-     */
-    public static final String SUBCLASS_TITLE = "(%s) %s";
-    /**
-     * 章节标题
-     */
-    public static final String CHAPTER_HEAD = "%d.%d  %s  %s%s  %s";
-
-    public static final Map<Integer, String> SECTION_PREFIX = new HashMap<Integer, String>() {{
-        put(1, "一");
-        put(2, "二");
-        put(3, "三");
-        put(4, "四");
-        put(5, "五");
-        put(6, "六");
-        put(7, "七");
-        put(8, "八");
-        put(9, "九");
-        put(10, "十");
-    }};
-
 
     @Transactional(rollbackFor = {java. lang. Exception.class})
     @Override
@@ -1042,6 +1015,43 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
         builder.writeln(content);
     }
 
+    /**
+     * 内容添加
+     *
+     * @param builder         文档对象
+     * @param fontName        字体名称
+     * @param styleIdentifier 设置样式  正文 标题1 标题2 StyleIdentifier.HEADING_1
+     * @param fontSize        字体大小
+     * @param isBold          是否加粗
+     * @param content         内容
+     * @param prefixContent   内容前缀 此处用来做首行缩进
+     * @throws Exception
+     */
+    private void buildTitleContent(DocumentBuilder builder, String fontName, int styleIdentifier, int fontSize,
+                                  boolean isBold, String content, String prefixContent) throws Exception {
+        // 设置字体
+        builder.getFont().setName(fontName);
+        // 是否倾斜
+        builder.getFont().setItalic(false);
+        // 设置字体大小
+        builder.getFont().setSize(fontSize);
+        // 是否加粗
+        builder.getFont().setBold(isBold);
+        // 设置对齐方式
+        builder.getParagraphFormat().setAlignment(ParagraphAlignment.LEFT);
+        // 设置样式
+        builder.getParagraphFormat().setStyleIdentifier(styleIdentifier);
+        // 设置前缀 此处作为 首行缩进
+        if (prefixContent != null) {
+            builder.write(prefixContent);
+        }
+        if (StringUtils.isBlank(content)) {
+            content = "";
+        }
+        // 添加正文内容
+        builder.writeln(content);
+    }
+
     /**
      * 内容添加
      *
@@ -1133,7 +1143,7 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
     }
 
     @Override
-    public int wordExportStandard(Map<String, Object> map, HttpServletResponse response) throws Exception{
+    public R wordExportStandard(Map<String, Object> map, HttpServletResponse response) throws Exception{
         SysFileItemDo fileItemDo = sysFileItemService.getById("a2ccde8da9ec485f83650b75429f0e78");
         final Document[] indexDoc = {WordHelpUtils.getDoc(fileItemDo.getFullpath())};
         FormData formData = new FormData();
@@ -1147,16 +1157,16 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
         List<ZhcxCommissionCheckBaseInfoTreeDo> treeList = zhcxCommissionCheckBaseInfoTreeDao.listTreeData(map);
         Document doc = null;
         DocumentBuilder builder = null;
-        ClassPathResource classPathResource = new ClassPathResource("template/试车标准内容明细.doc");
+        ClassPathResource classPathResource = new ClassPathResource("template/试车标准内容明细.docx");
         try {
             doc = new Document(classPathResource.getInputStream());
             builder = new DocumentBuilder(doc);
         } catch (Exception e) {
             ExceptionUtils.printExceptionDetail(e,"空模板获取出现异常");
-            return 0;
+            return R.error("空模板获取出现异常");
         }
         if(list.isEmpty()||treeList.isEmpty()){
-            return 1;
+            return R.error("该模板没有数据");
         }else {
             //标题
             Map<String, ZhcxCommissionCheckDetailStandardWordVO> resMap = new HashMap<>();
@@ -1219,20 +1229,21 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                 if (fillIdLength<=3){
                     key = treeFullId.substring(i+1);
                     if(!resMap.containsKey(key)){
-                        log.error("不能导入树表的id:{}的报告", treeFullId);
-                        log.error("不能导入树表的key:{}的报告", key);
-                        return 2;
+                        log.error("不能导出树表的id:{}的报告", treeFullId);
+                        log.error("不能导出树表的key:{}的报告", key);
+                        log.error("章节号有无误:{}的报告", item.getChapterno());
+                        return R.error(item.getChapterno()+"章节号有无误");
                     }
                 }else {
                     // 从第一个逗号之后开始找第二个逗号
                     int j = treeFullId.indexOf(',', i + 1);
                     key = treeFullId.substring(j+1);
                     if(!resMap.containsKey(key)){
-                        log.error("不能导入树表的id:{}的报告", treeFullId);
-                        log.error("不能导入树表的key:{}的报告", key);
-                        return 2;
+                        log.error("不能导出树表的id:{}的报告", treeFullId);
+                        log.error("不能导出树表的key:{}的报告", key);
+                        log.error("章节号有无误:{}的报告", item.getChapterno());
+                        return R.error(item.getChapterno()+"章节号有无误");
                     }
-
                 }
                 ZhcxCommissionCheckDetailStandardWordVO checkDetailWordVO = resMap.get(key);
                 List<ZhcxCommissionCheckBaseInfoDetailDo> detailList = checkDetailWordVO.getDetailList();
@@ -1250,11 +1261,13 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
             builder.moveToDocumentStart();
             for (ZhcxCommissionCheckDetailStandardWordVO res : resList) {
                 if (res.getLevel()==1){
-                    buildText(builder, DEFAULT_FONT_NAME, res.getLevel(), 12, true,
-                            res.getChapterno()+"."+res.getTitleContent(), null);
+                    buildTitleContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_1, 15, false, res.getChapterno()+"."+res.getTitleContent(), null);
                 }else {
-                    buildText(builder, DEFAULT_FONT_NAME, res.getLevel(), 12, true,
-                            res.getChapterno()+res.getTitleContent(), null);
+                    if (res.getLevel()==2){
+                        buildTitleContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_2, 15, false,res.getChapterno()+"."+res.getTitleContent(), null);
+                    }else {
+                        buildTitleContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_3, 15, false, res.getChapterno()+"."+res.getTitleContent(), null);
+                    }
                 }
                 if(ObjectUtil.isNotEmpty(res.getDetailList())){
                     builder.getFont().setSize(15);
@@ -1264,50 +1277,57 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                     builder.getRowFormat().setHeight(20);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "NO.", null);
+                    builder.getFont().setSize(9);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("NO.");
                     builder.insertCell();
                     builder.getCellFormat().setWidth(40);
                     builder.getRowFormat().setHeight(20);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "模式1", null);
+                    builder.getFont().setSize(9);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("模式1");
                     builder.insertCell();
                     builder.getCellFormat().setWidth(40);
                     builder.getRowFormat().setHeight(20);
+                    builder.getFont().setSize(9);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "模式2", null);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("模式2");
                     builder.insertCell();
                     builder.getCellFormat().setWidth(60);
                     builder.getRowFormat().setHeight(20);
+                    builder.getFont().setSize(9);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "测试内容中文", null);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("测试内容中文");
                     builder.insertCell();
                     builder.getCellFormat().setWidth(60);
                     builder.getRowFormat().setHeight(20);
+                    builder.getFont().setSize(9);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "测试内容英文", null);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("测试内容英文");
                     builder.insertCell();
                     builder.getCellFormat().setWidth(60);
                     builder.getRowFormat().setHeight(20);
+                    builder.getFont().setSize(9);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "标准测试方法", null);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("标准测试方法");
                     builder.insertCell();
                     builder.getCellFormat().setWidth(60);
                     builder.getRowFormat().setHeight(20);
+                    builder.getFont().setSize(9);
                     builder.getRowFormat().setHeightRule(HeightRule.EXACTLY);//设置HeightRule
                     builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                    buildTextContentTitle(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_6, 12, true,
-                            "测试地点", null);
+                    builder.getParagraphFormat().setStyle(doc.getStyles().get("Normal"));
+                    builder.writeln("测试地点");
                     builder.endRow();
                     for (ZhcxCommissionCheckBaseInfoDetailDo detailWordVO:res.getDetailList()){
                         // 插入表格的第一行
@@ -1315,36 +1335,33 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                         builder.getCellFormat().setWidth(30);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
                         builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                detailWordVO.getChapterno(), null);
+                        String chapterno = detailWordVO.getChapterno()+"";
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false,chapterno, null);
                         builder.insertCell();
                         builder.getCellFormat().setWidth(40);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
                         builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,detailWordVO.getClassification(), null);
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false,detailWordVO.getClassification(), null);
                         builder.insertCell();
                         builder.getCellFormat().setWidth(40);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
                         builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,detailWordVO.getClassificationtwo(), null);
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false,detailWordVO.getClassificationtwo(), null);
                         builder.insertCell();
                         builder.getCellFormat().setWidth(60);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
                         builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                detailWordVO.getChtestcontent(), null);
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false, detailWordVO.getChtestcontent(), null);
                         builder.insertCell();
                         builder.getCellFormat().setWidth(60);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
                         builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                detailWordVO.getEntestcontent(), null);
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false, detailWordVO.getEntestcontent(), null);
                         builder.insertCell();
                         builder.getCellFormat().setWidth(60);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
                         builder.getCellFormat().setOrientation(TextOrientation.HORIZONTAL);
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_7, 11, false,
-                                detailWordVO.getTestmethod(), null);
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false, detailWordVO.getTestmethod(), null);
                         builder.insertCell();
                         builder.getCellFormat().setWidth(60);
                         builder.getRowFormat().setHeightRule(HeightRule.AUTO);//设置HeightRule
@@ -1356,19 +1373,26 @@ public class ZhcxCommissionCheckServiceImpl extends ServiceImpl<ZhcxCommissionCh
                                 testaddress = "USER SITE码头现场";
                             }
                         }
-                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.HEADING_8, 9, false,
-                                testaddress, null);
+                        buildTextContent(builder, DEFAULT_FONT_NAME, StyleIdentifier.NORMAL, 9, false, testaddress, null);
                         builder.endRow();
                     }
                     // 结束表格
                     builder.endTable();
                 }
             }
-            indexDoc[0].appendDocument(doc, ImportFormatMode.KEEP_DIFFERENT_STYLES);
-            // 导出world
-            exportWord(indexDoc[0], response);
+            // 创建自定义样式
+            Style customStyle = indexDoc[0].getStyles().add(StyleType.PARAGRAPH, "CustomStyle");
+            customStyle.getFont().setName("宋体");
+            customStyle.getFont().setSize(12);
+
+            // 创建模板A的副本
+            Document cloneDocA = indexDoc[0].deepClone();
+            // 在副本上合并文档B
+            cloneDocA.appendDocument(doc, ImportFormatMode.KEEP_SOURCE_FORMATTING);
+            //导出world
+            exportWord(cloneDocA, response);
         }
-        return 3;
+        return R.ok("导出成功");
     }
 
     /**

+ 5 - 10
business-server/src/main/java/com/rongwei/bsserver/controller/ZhcxCommissionCheckController.java

@@ -81,16 +81,11 @@ public class ZhcxCommissionCheckController {
 
     @PostMapping("wordExportStandard")
     @ApiOperation("试车标准库word导出/预览")
-    public void wordExportStandard(@RequestBody Map<String,Object> map, HttpServletResponse response) throws Exception {
-        int i = commissionCheckService.wordExportStandard(map,response);
-        if(i == 0){
-            throw new CustomException("程序异常");
-        }else if(i == 1){
-            throw new CustomException("检查内容暂无数据");
-        }else {
-            if (i==2){
-                throw new CustomException("存在章节号错误数据");
-            }
+    public R wordExportStandard(@RequestBody Map<String,Object> map, HttpServletResponse response) throws Exception {
+        try {
+            return commissionCheckService.wordExportStandard(map,response);
+        } catch (Exception e) {
+            return R.error("试车报告导出异常");
         }
     }
 

business-server/src/main/resources/template/试车标准内容明细.doc → business-server/src/main/resources/template/试车标准内容明细.docx