Browse Source

word填充

钱惠东 1 month ago
parent
commit
b86bd43167

+ 14 - 0
RuoYi-Vue-fast-master/pom.xml

@@ -356,6 +356,20 @@
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-freemarker</artifactId>
         </dependency>
+
+        <!-- word文档生成及导出pdf -->
+        <dependency>
+            <groupId>fr.opensagres.xdocreport</groupId>
+            <artifactId>xdocreport</artifactId>
+            <version>2.0.1</version>
+        </dependency>
+
+        <dependency>
+            <groupId>fr.opensagres.xdocreport</groupId>
+            <artifactId>fr.opensagres.xdocreport.template.freemarker</artifactId>
+            <version>2.0.1</version>
+        </dependency>
+
     </dependencies>
 
     <build>

+ 143 - 0
RuoYi-Vue-fast-master/src/main/java/com/ruoyi/common/utils/office/doc/CommonDocUtils.java

@@ -0,0 +1,143 @@
+package com.ruoyi.common.utils.office.doc;
+
+import cn.hutool.core.collection.CollectionUtil;
+import cn.hutool.core.util.ReflectUtil;
+import fr.opensagres.xdocreport.core.XDocReportException;
+import fr.opensagres.xdocreport.document.IXDocReport;
+import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
+import fr.opensagres.xdocreport.template.IContext;
+import fr.opensagres.xdocreport.template.TemplateEngineKind;
+import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.*;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.UUID;
+
+
+@Slf4j
+public class CommonDocUtils {
+
+    private IXDocReport report;
+    private IContext context;
+    private FieldsMetadata metadata;
+
+    /***************************************** ↓初始文件加载↓ ********************************************************/
+
+    /**
+     * 从类路径加载
+     * @param path 文件路径
+     * @return
+     */
+    public CommonDocUtils loadFromClasspath(String path) {
+        try (InputStream is = this.getClass().getResourceAsStream(path)) {
+            report = XDocReportRegistry.getRegistry().loadReport(is, UUID.randomUUID().toString(), TemplateEngineKind.Freemarker );
+            context = report.createContext();
+            metadata = report.createFieldsMetadata();
+        } catch (FileNotFoundException e) {
+            throw new RuntimeException(e);
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        } catch (XDocReportException e) {
+            throw new RuntimeException(e);
+        }
+        return this;
+    }
+
+    /***************************************** ↓文件内容修改↓ ********************************************************/
+
+    /**
+     * 文本替换  !!!!!${placeholder}必须从文本编辑器复制到word里,不能直接在word里输入,不然会被分割成多段导致本方法无法替换!!!
+     * @param data
+     * @return
+     */
+    public CommonDocUtils replaceData(Map<String, String> data){
+        if (CollectionUtil.isEmpty(data)){
+            return this;
+        }
+        data.keySet().forEach(key -> {
+            context.put(key, data.get(key));
+        });
+        return this;
+    }
+
+
+    /**
+     * 替换表格数据
+     * @param data
+     * @return
+     */
+    public CommonDocUtils replaceTableData(Map<String, List<List<String>>> data){
+        if (CollectionUtil.isEmpty(data)){
+            return this;
+        }
+        data.keySet().forEach(key -> {
+            try {
+                metadata.load(key, CommonTableRow.class, true );
+                List<List<String>> tableData = data.get(key);
+                List<CommonTableRow> rows = new ArrayList<>();
+                for (int i=0;i<tableData.size();i++){
+                    List<String> rowData = tableData.get(i);
+                    CommonTableRow row = new CommonTableRow();
+                    rows.add(row);
+                    for (int j=0;j<rowData.size();j++){
+                        ReflectUtil.setFieldValue(row, "col" + j, rowData.get(j));
+                    }
+                }
+                context.put(key, rows);
+            } catch (XDocReportException e) {
+                throw new RuntimeException(e);
+            }
+        });
+
+        return this;
+    }
+
+    /***************************************** ↓获取最终文件↓ ********************************************************/
+//
+//    /**
+//     * 将word转存为pdf
+//     * @param path pdf文件路径
+//     */
+//    public void saveToPdf(String path) {
+//        DocumentConverter converter = null;
+//        try {
+//            converter = SpringContextHolder.getBean(DocumentConverter.class);
+//        } catch (Exception e){
+//            throw new ServiceErrorException("未找到有效的office程序,无法生成pdf文件");
+//        }
+//
+//        String basePath = path.substring(0, path.lastIndexOf("/"));
+//        //指定文件上传的目录
+//        File file1 = new File(basePath);
+//        //如果目录不存在 递归创建
+//        if(!file1.exists()){
+//            file1.mkdirs();
+//        }
+//        File temp = null;
+//        try {
+//            temp = File.createTempFile("temp", ".tmp");
+//        } catch (IOException e) {
+//            throw new RuntimeException(e);
+//        }
+//        try(OutputStream tempOut = new FileOutputStream(temp);
+//            InputStream tempIn = new FileInputStream(temp);
+//            OutputStream out = new FileOutputStream(path);){
+//            long start = System.currentTimeMillis();
+//            report.process(context, tempOut);
+//
+//            long wordEnd = System.currentTimeMillis();
+//            final DocumentFormat targetFormat = DefaultDocumentFormatRegistry.getFormatByExtension("pdf");
+//            converter.convert(tempIn).to(out).as(targetFormat).execute();
+//
+//            temp.delete();
+//            long pdfEnd = System.currentTimeMillis();
+//            log.info("PDF文件生成成功 路径:{} word生成耗时:{}ms pdf转换耗时:{}ms", path, wordEnd-start, pdfEnd-wordEnd);
+//        } catch (Exception e) {
+//            throw new RuntimeException(e);
+//        }
+//    }
+
+}

+ 20 - 0
RuoYi-Vue-fast-master/src/main/java/com/ruoyi/common/utils/office/doc/CommonTableRow.java

@@ -0,0 +1,20 @@
+package com.ruoyi.common.utils.office.doc;
+
+import lombok.Data;
+
+@Data
+public class CommonTableRow {
+
+    private String col0 = "";
+    private String col1 = "";
+    private String col2 = "";
+    private String col3 = "";
+    private String col4 = "";
+    private String col5 = "";
+    private String col6 = "";
+    private String col7 = "";
+    private String col8 = "";
+    private String col9 = "";
+    private String col10 = "";
+
+}