From 52f98f1a31e682f725f09b4a37bcd316332d3131 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期四, 01 八月 2024 20:06:15 +0800
Subject: [PATCH] 优化word报告生成

---
 src/main/java/com/whyc/service/ReportService.java |   51 +++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 51 insertions(+), 0 deletions(-)

diff --git a/src/main/java/com/whyc/service/ReportService.java b/src/main/java/com/whyc/service/ReportService.java
index f8b990a..a018113 100644
--- a/src/main/java/com/whyc/service/ReportService.java
+++ b/src/main/java/com/whyc/service/ReportService.java
@@ -1,9 +1,20 @@
 package com.whyc.service;
 
+import com.whyc.dto.Response;
 import com.whyc.mapper.ReportMapper;
+import com.whyc.util.CommonUtil;
+import org.docx4j.Docx4J;
+import org.docx4j.openpackaging.exceptions.Docx4JException;
+import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
+import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
+import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 
 import javax.annotation.Resource;
+import javax.xml.bind.JAXBException;
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
 
 @Service
 public class ReportService {
@@ -11,4 +22,44 @@
     @Resource
     private ReportMapper mapper;
 
+    @Autowired
+    private BattRTStateService battRTStateService;
+
+    public Response<String> generateDocument() throws JAXBException, Docx4JException {
+        Response<String> response = new Response<>();
+        //鑾峰彇classpath涓嬬殑妯℃澘
+        String templatePath = this.getClass().getClassLoader().getResource("").getPath()+"\\template\\template_01.docx";
+        //鍔犺浇妯℃澘
+        WordprocessingMLPackage wordprocessingMLPackage = WordprocessingMLPackage.load(new File(templatePath));
+        MainDocumentPart documentPart =  wordprocessingMLPackage.getMainDocumentPart();
+
+        //鏌ヨ宸℃鏃跺�欑殑鐢垫祦鐢靛帇娓╁害鏁版嵁
+        float current = 1.0f;
+        float voltage = 2.0f;
+        float temperature = 3.0f;
+        String reporter = "whyc";
+        String inspectionTime = "2020-01-01 00:00:00";
+        HashMap<String,String> map = new HashMap<>();
+        map.put("current",current+"");
+        map.put("voltage",voltage+"");
+        map.put("temperature",temperature+"");
+        map.put("reporter",reporter);
+        map.put("inspectionTime",inspectionTime);
+        //鏇挎崲鍗犱綅绗�
+        documentPart.variableReplace(map);
+        //杈撳嚭鏂囦欢鍚嶇О
+        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
+        String fileName = "鎶ュ憡_01_"+format.format(System.currentTimeMillis())+".docx";
+        String fileDir = CommonUtil.getRootFile()+"鎶ュ憡";
+        File dirFile = new File(fileDir);
+        if(!dirFile.exists()){
+            dirFile.mkdirs();
+        }
+        String filePath = fileDir + File.separator + fileName;
+        Docx4J.save(wordprocessingMLPackage, new File(filePath));
+        response.setCode(1);
+        response.setData("\\inspectionSystem_file\\鎶ュ憡\\"+fileName);
+        return response;
+
+    }
 }

--
Gitblit v1.9.1