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

---
 src/main/java/com/whyc/controller/ReportController.java |   44 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 44 insertions(+), 0 deletions(-)

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();
+        }
+
+    }
+
 }

--
Gitblit v1.9.1