Przeglądaj źródła

poi word模板生成word

LIVE_YE 1 rok temu
rodzic
commit
00bcdabdf5

+ 2 - 1
pom.xml

@@ -27,7 +27,8 @@
         <fastjson.version>2.0.43</fastjson.version>
         <oshi.version>6.5.0</oshi.version>
         <commons.io.version>2.13.0</commons.io.version>
-        <poi.version>4.1.2</poi.version>
+       <!-- <poi.version>4.1.2</poi.version>-->
+        <poi.version>5.2.3</poi.version>
         <velocity.version>2.3</velocity.version>
         <jwt.version>0.9.1</jwt.version>
     </properties>

+ 6 - 0
ruoyi-common/pom.xml

@@ -133,6 +133,12 @@
             <artifactId>javax.servlet-api</artifactId>
         </dependency>
 
+        <dependency>
+            <groupId>com.deepoove</groupId>
+            <artifactId>poi-tl</artifactId>
+            <version>1.12.2</version>
+        </dependency>
+
     </dependencies>
 
 </project>

+ 85 - 0
ruoyi-common/src/main/java/com/ruoyi/common/utils/poi/WordUtil.java

@@ -0,0 +1,85 @@
+package com.ruoyi.common.utils.poi;
+
+import com.deepoove.poi.XWPFTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.Assert;
+
+import java.io.File;
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * @author Jerry
+ * @Title: WordUtil
+ * @Description: Word工具类
+ * @date 219/10/6 9:09
+ */
+public class WordUtil {
+
+    private static Logger logger = LoggerFactory.getLogger(WordUtil.class);
+
+
+    /**
+     * 根据模板填充内容生成word
+     * 调用方法参考下面的main方法,详细文档参考官方文档
+     * Poi-tl模板引擎官方文档:http://deepoove.com/poi-tl/
+     *
+     * @param templatePath word模板文件路径
+     * @param fileDir      生成的文件存放地址
+     * @param fileName     生成的文件名,不带格式。假如要生成abc.docx,则fileName传入abc即可
+     * @param paramMap     替换的参数集合
+     * @return 生成word成功返回生成的文件的路径,失败返回空字符串
+     */
+    public static String createWord(String templatePath, String fileDir, String fileName, Map<String, Object> paramMap) {
+        Assert.notNull(templatePath, "word模板文件路径不能为空");
+        Assert.notNull(fileDir, "生成的文件存放地址不能为空");
+        Assert.notNull(fileName, "生成的文件名不能为空");
+
+        // 生成的word格式
+        String formatSuffix = ".docx";
+        // 拼接后的文件名
+        fileName = fileName + formatSuffix;
+
+        // 生成的文件的存放路径
+        if (!fileDir.endsWith("/")) {
+            fileDir = fileDir + File.separator;
+        }
+
+        File dir = new File(fileDir);
+        if (!dir.exists()) {
+            logger.info("生成word数据时存储文件目录{}不存在,为您创建文件夹!", fileDir);
+            dir.mkdirs();
+        }
+
+        String filePath = fileDir + fileName;
+        // 读取模板templatePath并将paramMap的内容填充进模板,即编辑模板+渲染数据
+        XWPFTemplate template = XWPFTemplate.compile(templatePath).render(paramMap);
+        try {
+            // 将填充之后的模板写入filePath
+            template.writeToFile(filePath);
+            template.close();
+        } catch (Exception e) {
+            logger.error("生成word异常", e);
+            e.printStackTrace();
+            return "";
+        }
+        return filePath;
+    }
+
+    public static void main(String[] args) {
+        Map<String, Object> params = new HashMap<>();
+        // 渲染文本
+        params.put("title", "XXX工程");
+        //...
+        // 渲染图片
+        //params.put("picture", new PictureRenderData(120, 120, "D:\\wx.png"));
+        // TODO 渲染其他类型的数据请参考官方文档
+        String templatePath = "D:\\zdd.docx";
+        String fileDir = "D:\\template";
+        String fileName = "zdd2";
+
+        String wordPath = WordUtil.createWord(templatePath, fileDir, fileName, params);
+        System.out.println("生成文档路径:" + wordPath);
+    }
+}