Java-使用poi-tl根据word模板动态生成word

作者wangsz,想写一些关于word的工具,所以就写了这篇文章

1.首先,先导入所需要的依赖(poi相关依赖即可)

复制代码
        <!-- POI -->
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml</artifactId>
            <version>4.1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi-ooxml-schemas</artifactId>
            <version>4.1.2</version>
        </dependency>

        <!-- poi-tl -->
        <dependency>
            <groupId>com.deepoove</groupId>
            <artifactId>poi-tl</artifactId>
            <version>1.9.1</version>
        </dependency>




        <!-- https://mvnrepository.com/artifact/org.apache.pdfbox/pdfbox -->
        <dependency>
            <groupId>org.apache.pdfbox</groupId>
            <artifactId>pdfbox</artifactId>
            <version>2.0.24</version>
        </dependency>

        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itextpdf</artifactId>
            <version>5.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.itextpdf</groupId>
            <artifactId>itext-asian</artifactId>
            <version>5.2.0</version>
        </dependency>



        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

2.依赖导入完成后,先了解一些poi基础知识,制作word模板

{{template}}

普通文本,渲染数据为:String或者TextRenderData

{{@template}}

图片,渲染数据为:PictureRenderData

{{#template}}

表格,渲染数据为:TableRenderData

{{*template}}

列表,渲染数据为:NumbericRenderData

3.编写生成word文档代码

java 复制代码
 //将图片转换成字节数组
    public static byte[] getUrlByteArray(String imageUrl) throws IOException {
        URL url = new URL(imageUrl);
        try (InputStream inputStream = url.openStream();
             ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                outputStream.write(buffer, 0, bytesRead);
            }
            return outputStream.toByteArray();
        }
    }
// 主要工具
public void testPicture(){

      
        Map<String, Object> datas = new HashMap() {
            {
                //本地图片
         //     put("photo", new PictureRenderData(100, 116, "C:\\Users\\53129\\Pictures\\Saved Pictures\\111.png") );
                put("name","王首zhen");
              //  put("oname","");
                put("sex","男");
                put("nation","汉族");
                put("address","天津市");
//				//本地图片byte数据
//				put("localBytePicture", new PictureRenderData(100, 120, ".png", BytePictureUtils.getLocalByteArray(new File("src/test/resources/logo.png"))));
//				//网路图片
                try {
                    put("photo", new PictureRenderData(100, 100, ".png", testPotPi.getUrlByteArray("https://avatars3.githubusercontent.com/u/1394854?v=3&s=40")));
                } catch (IOException e) {
                    e.printStackTrace();
                }
//				// java 图片
//				put("bufferImagePicture", new PictureRenderData(100, 120, ".png", BytePictureUtils.getBufferByteArray(bufferImage)));
            }
        };

        XWPFTemplate template = XWPFTemplate.compile("C:\\Users\\53129\\Desktop\\模板文件.docx")
                .render(datas);
        FileOutputStream out;
        try {
            out = new FileOutputStream("C:\\Users\\53129\\Desktop\\生成文件.docx");
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

4.测试代码

java 复制代码
  public static void main(String[] args) {
        long l = System.currentTimeMillis();
        new testPotPi().testPicture();
        long l1 = System.currentTimeMillis();
        System.err.println("运行时长"+(l1-l));
    }

5.封装工具类

java 复制代码
   /*
   tempath :模板文件地址
   decPath : 生成文件地址
    */
    public static void testPicture(Map<String, Object> datas , String temPath , String decPath){
        
        XWPFTemplate template = XWPFTemplate.compile(temPath)
                .render(datas);
        FileOutputStream out;
        try {
            out = new FileOutputStream(decPath);
            template.write(out);
            out.flush();
            out.close();
            template.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
相关推荐
南部余额3 分钟前
踩坑与解惑:深入理解 SpringBoot 自动配置原理与配置排除机制
java·spring boot·自动配置原理·import
CHANG_THE_WORLD25 分钟前
Python 字符串全面解析
开发语言·python
不会c嘎嘎35 分钟前
深入理解 C++ 异常机制:从原理到工程实践
开发语言·c++
永远都不秃头的程序员(互关)1 小时前
C语言 基本语法
c语言·开发语言
木鹅.1 小时前
LangChain4j
java
永远都不秃头的程序员(互关)1 小时前
Java核心技术精要:高效实践指南
java·开发语言·性能优化
是Dream呀2 小时前
Python圣诞特辑:打造一棵会唱歌、会下雪的魔法圣诞树
开发语言·python·pygame
未来之窗软件服务2 小时前
幽冥大陆(四十一)美萍V10酒店门锁SDK C#语言仙盟插件——东方仙盟筑基期
开发语言·c#·仙盟创梦ide·东方仙盟·东方仙盟sdk·酒店智能门锁·东方仙盟 vos 智能浏览器
CoderYanger2 小时前
动态规划算法-子序列问题(数组中不连续的一段):28.摆动序列
java·算法·leetcode·动态规划·1024程序员节
代码栈上的思考2 小时前
深入解析Spring IoC核心与关键注解
java·后端·spring