Jelajahi Sumber

增加poi-tl导出word模板

wukai 6 bulan lalu
induk
melakukan
326721e051

+ 1 - 1
jjt-admin/src/main/resources/bootstrap.yml

@@ -8,7 +8,7 @@ spring:
       servicecomb:
          discovery:
             #servicecomb 根据实际地址修改
-            address: http://127.0.0.1:30100
+            address: http://192.168.188.66:30100
             serviceName: ${spring.application.name}
             appName: pxf-risk-monitor
             watch: false

+ 34 - 0
jjt-admin/src/test/java/com/doc/DocTest.java

@@ -0,0 +1,34 @@
+package com.doc;
+
+import com.deepoove.poi.XWPFTemplate;
+
+import java.io.*;
+import java.util.HashMap;
+
+/**
+ * DocTest$
+ *
+ * @author wukai
+ * @date 2024/12/3 23:43
+ */
+public class DocTest {
+    public static void main(String[] args) throws FileNotFoundException {
+        // 定义模板对应的数据
+        HashMap<String, Object> data = new HashMap<>();
+        data.put("test", "张三");
+        data.put("age", "正常■ 异常□");
+
+        // 加载本地模板文件
+//        InputStream inputStream = getClass().getResourceAsStream("/演示模板1.docx");
+        FileInputStream inputStream = new FileInputStream("D:\\SYSTEM\\Desktop\\temp\\doc\\check.docx");
+        // 渲染模板
+        XWPFTemplate template = XWPFTemplate.compile(inputStream).render(data);
+
+        try {
+            // 写出到文件
+            template.writeAndClose(new FileOutputStream("D:\\SYSTEM\\Desktop\\temp\\doc\\output.docx"));
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}

+ 5 - 0
jjt-biz/pom.xml

@@ -16,6 +16,11 @@
     </description>
 
     <dependencies>
+        <dependency>
+            <groupId>com.deepoove</groupId>
+            <artifactId>poi-tl</artifactId>
+            <version>1.10.6</version>
+        </dependency>
         <!-- 核心模块-->
         <dependency>
             <groupId>com.jjt</groupId>

+ 40 - 0
jjt-biz/src/main/java/com/jjt/biz/controller/BizModelController.java

@@ -1,10 +1,12 @@
 package com.jjt.biz.controller;
 
+import com.deepoove.poi.XWPFTemplate;
 import com.github.pagehelper.PageHelper;
 import com.jjt.biz.domain.BizModel;
 import com.jjt.biz.domain.BizObj;
 import com.jjt.biz.service.IBizModelService;
 import com.jjt.biz.util.DataUtil;
+import com.jjt.biz.vo.CheckExportVO;
 import com.jjt.biz.vo.CheckVO;
 import com.jjt.biz.vo.ScoreVO;
 import com.jjt.common.annotation.Log;
@@ -25,6 +27,8 @@ import org.springframework.web.bind.annotation.*;
 
 import javax.annotation.Resource;
 import javax.servlet.http.HttpServletResponse;
+import java.io.*;
+import java.net.URLEncoder;
 import java.time.LocalDate;
 import java.time.LocalDateTime;
 import java.time.format.DateTimeFormatter;
@@ -321,6 +325,42 @@ public class BizModelController extends BaseController {
         return AjaxResult.success(list);
     }
 
+    @ApiOperation("一健巡检--导出报告")
+    @GetMapping("/check/export")
+    public void checkExport(HttpServletResponse response) {
+        CheckExportVO vo = new CheckExportVO();
+        vo.mock();
+        // 定义模板对应的数据
+        HashMap<String, Object> data = new HashMap<>();
+        data.put("test", "测试dsx哈");
+        for (int i = 1; i <= 28; i++) {
+            data.put("l" + i, vo.getLeft()[i - 1]);
+            data.put("r" + i, vo.getRight()[i - 1]);
+        }
+        // 渲染模板
+        try (InputStream inputStream = getClass().getClassLoader().getResourceAsStream("tpl/check.docx");
+             OutputStream outputStream = new BufferedOutputStream(response.getOutputStream());
+             ByteArrayOutputStream out = new ByteArrayOutputStream();) {
+            // 写出到文件
+            XWPFTemplate template = XWPFTemplate.compile(inputStream).render(data);
+
+            template.writeAndClose(out);
+            // 清空response
+            response.reset();
+            // 设置response的Header
+            response.setCharacterEncoding("UTF-8");
+            //Content-Disposition的作用:告知浏览器以何种方式显示响应返回的文件,用浏览器打开还是以附件的形式下载到本地保存
+            //attachment表示以附件方式下载 inline表示在线打开 "Content-Disposition: inline; filename=文件名.mp3"
+            // filename表示文件的默认名称,因为网络传输只支持URL编码的相关支付,因此需要将文件名URL编码后进行传输,前端收到后需要反编码才能获取到真正的名称
+            response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode("新一代电力交易平台巡检报告" + DateUtils.dateTimeNow() + ".docx", "UTF-8"));
+            response.setContentType("application/octet-stream");
+            outputStream.write(out.toByteArray());
+            outputStream.flush();
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+
     @ApiOperation("一健巡检--查看报告分类明细")
     @GetMapping("/check/proj/{id}")
     public AjaxResult checkProj(@PathVariable("id") Long id) {

+ 22 - 0
jjt-biz/src/main/java/com/jjt/biz/vo/CheckExportVO.java

@@ -0,0 +1,22 @@
+package com.jjt.biz.vo;
+
+import io.swagger.annotations.ApiModel;
+import lombok.Data;
+
+/**
+ * CheckExportVO$
+ *
+ * @author wukai
+ * @date 2024/12/4 01:31
+ */
+@ApiModel(value = "CheckExportVO", description = "巡检导出")
+@Data
+public class CheckExportVO {
+    private String[] left;
+    private String[] right;
+
+    public void mock() {
+        this.left = new String[]{"11.91", "22.15", "33.18", "44.14", "55.33", "正常■ 异常□", "正常■ 异常□", "正常■ 异常□", "正常■ 异常□", "14.07", "89.7", "45.01", "1010", "正常■ 异常□", "正常■ 异常□", "正常■ 异常□", "11", "正常■ 异常□", "1233.51", "正常□ 异常■", "正常■ 异常□", "正常■ 异常□", "正常■ 异常□", "是■ 否□", "正常■ 异常", "正常■ 异常□", "是■ 否□", "正常■ 异常□"};
+        this.right = new String[]{"正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常", "正常",};
+    }
+}

TEMPAT SAMPAH
jjt-biz/src/main/resources/tpl/check.docx


+ 1 - 1
jjt-framework/src/main/java/com/jjt/framework/config/SecurityConfig.java

@@ -111,7 +111,7 @@ public class SecurityConfig extends WebSecurityConfigurerAdapter {
                 // 过滤请求
                 .authorizeRequests()
                 // 对于登录login 注册register 验证码captchaImage 允许匿名访问
-                .antMatchers("/api/login", "/api/loginSso", "/register", "/captchaImage").permitAll()
+                .antMatchers("/api/login", "/api/loginSso", "/hl/bm/check/export", "/register", "/captchaImage").permitAll()
                 // 静态资源,可匿名访问
                 .antMatchers(HttpMethod.GET, "/", "/*.html", "/**/*.html", "/**/*.css", "/**/*.js", "/profile/**").permitAll()
                 .antMatchers("/swagger-ui.html", "/swagger-resources/**", "/webjars/**", "/*/api-docs", "/druid/**").permitAll()

+ 1 - 1
pom.xml

@@ -260,4 +260,4 @@
 		</pluginRepository>
 	</pluginRepositories>
 
-</project>
+</project>