From bd1fede9ba085ce808e75e8a6af2e45802f14e28 Mon Sep 17 00:00:00 2001
From: whycxzp <glperry@163.com>
Date: 星期四, 01 八月 2024 19:25:35 +0800
Subject: [PATCH] 更新word报告生成

---
 /dev/null                                               |   66 ----------------------
 src/main/resources/template/template_01.docx            |    0 
 src/main/resources/config/application-dev.yml           |   12 ++-
 src/main/java/com/whyc/controller/ReportController.java |   44 ++++++++++++++
 pom.xml                                                 |    5 +
 5 files changed, 56 insertions(+), 71 deletions(-)

diff --git a/pom.xml b/pom.xml
index e98fe2a..d8bc07a 100644
--- a/pom.xml
+++ b/pom.xml
@@ -115,6 +115,11 @@
             <artifactId>javase</artifactId>
             <version>3.5.3</version>
         </dependency>
+        <dependency>
+            <groupId>org.docx4j</groupId>
+            <artifactId>docx4j</artifactId>
+            <version>6.1.2</version>
+        </dependency>
 
     </dependencies>
 
diff --git a/src/main/java/com/whyc/controller/ReportController.java b/src/main/java/com/whyc/controller/ReportController.java
index 5d153f9..302bec9 100644
--- a/src/main/java/com/whyc/controller/ReportController.java
+++ b/src/main/java/com/whyc/controller/ReportController.java
@@ -1,9 +1,19 @@
 package com.whyc.controller;
 
 import com.whyc.service.ReportService;
+import org.docx4j.Docx4J;
+import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
+import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
 import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 import org.springframework.web.bind.annotation.RestController;
+
+import java.io.File;
+import java.text.SimpleDateFormat;
+import java.util.HashMap;
+
+import static org.docx4j.Docx4J.*;
 
 /**
  * 鎶ュ憡鐢熸垚
@@ -15,4 +25,38 @@
     @Autowired
     private ReportService service;
 
+    @GetMapping("generateDocument")
+    public void generateDocument(){
+        try {
+            //鑾峰彇classpath涓嬬殑妯℃澘
+            String templatePath = this.getClass().getClassLoader().getResource("").getPath()+"resources\\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";
+            Docx4J.save(wordprocessingMLPackage, new File("C:\\code\\web\\InspectionSystem\\src\\main\\resources\\template\\"+fileName));
+
+        }catch(Exception e){
+            e.printStackTrace();
+        }
+
+    }
+
 }
diff --git a/src/main/java/com/whyc/mapper/PersonMapper.java b/src/main/java/com/whyc/mapper/PersonMapper.java
deleted file mode 100644
index 945e122..0000000
--- a/src/main/java/com/whyc/mapper/PersonMapper.java
+++ /dev/null
@@ -1,6 +0,0 @@
-package com.whyc.mapper;
-
-import com.whyc.pojo.Person;
-
-public interface PersonMapper extends CustomMapper<Person>{
-}
diff --git a/src/main/java/com/whyc/pojo/Person.java b/src/main/java/com/whyc/pojo/Person.java
deleted file mode 100644
index cdbd276..0000000
--- a/src/main/java/com/whyc/pojo/Person.java
+++ /dev/null
@@ -1,66 +0,0 @@
-package com.whyc.pojo;
-
-import com.baomidou.mybatisplus.annotation.TableField;
-import com.baomidou.mybatisplus.annotation.TableName;
-
-/**
- * "PERSONID" INT IDENTITY(1, 1) NOT NULL,
- * "SEX" CHAR(1) NOT NULL,
- * "NAME" VARCHAR(50) NOT NULL,
- * "EMAIL" VARCHAR(50),
- * "PHONE" VARCHAR(25),
- */
-@TableName(schema = "person",value = "person")
-public class Person {
-
-    @TableField("PERSONID")
-    private Integer personId;
-    @TableField("SEX")
-    private String sex;
-    @TableField("NAME")
-    private String name;
-    @TableField("EMAIL")
-    private String email;
-    @TableField("PHONE")
-    private String phone;
-
-    public Integer getPersonId() {
-        return personId;
-    }
-
-    public void setPersonId(Integer personId) {
-        this.personId = personId;
-    }
-
-    public String getSex() {
-        return sex;
-    }
-
-    public void setSex(String sex) {
-        this.sex = sex;
-    }
-
-    public String getName() {
-        return name;
-    }
-
-    public void setName(String name) {
-        this.name = name;
-    }
-
-    public String getEmail() {
-        return email;
-    }
-
-    public void setEmail(String email) {
-        this.email = email;
-    }
-
-    public String getPhone() {
-        return phone;
-    }
-
-    public void setPhone(String phone) {
-        this.phone = phone;
-    }
-}
diff --git a/src/main/resources/config/application-dev.yml b/src/main/resources/config/application-dev.yml
index c616905..fb86b5e 100644
--- a/src/main/resources/config/application-dev.yml
+++ b/src/main/resources/config/application-dev.yml
@@ -19,11 +19,13 @@
 spring:
   datasource:
     type: com.alibaba.druid.pool.DruidDataSource
-    driver-class-name: com.highgo.jdbc.Driver
-#    url: jdbc:dm://localhost:5236/SYSDBA?rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
-    url: jdbc:highgo://192.168.10.183:5866/highgo?rewriteBatchedStatements=true&zeroDateTimeBehavior=convertToNull&useUnicode=true&characterEncoding=utf-8
-    username: sysdba
-    password: Lmx&8688139
+    driver-class-name: com.mysql.cj.jdbc.Driver
+    #    url: jdbc:mysql://localhost:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true&useSSL=false
+    url: jdbc:mysql://192.168.10.79:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true
+    #    url: jdbc:mysql://192.168.10.80:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true
+    #    url: jdbc:mysql://118.89.139.230:3360/db_user?characterEncoding=utf8&serverTimezone=Asia/Shanghai&allowMultiQueries=true&rewriteBatchedStatements=true
+    username: root
+    password: lmx8688139
     maxIdel: 60
     initialPoolSize: 2
     minPoolSize: 5
diff --git a/src/main/resources/template/template_01.docx b/src/main/resources/template/template_01.docx
new file mode 100644
index 0000000..ac055dd
--- /dev/null
+++ b/src/main/resources/template/template_01.docx
Binary files differ

--
Gitblit v1.9.1