当前位置: 代码网 > it编程>编程语言>Java > Java poi-tl根据模板生成word文件的完整指南

Java poi-tl根据模板生成word文件的完整指南

2025年12月30日 Java 我要评论
基础操作本质上是通过占位符进行内容替换本文章仅操作docx格式的文档.doc (word 97-2003): 使用ole2格式,对应poi的 hwpf 组件.docx (word 2007+): 使用

基础操作

本质上是通过占位符进行内容替换

本文章仅操作docx格式的文档

  • .doc (word 97-2003): 使用ole2格式,对应poi的 hwpf 组件
  • .docx (word 2007+): 使用ooxml格式,对应poi的 xwpf 组件

基础操作_模板部分

将模板放入resources 资源目录下,自定义文件夹中,例如:templet/word_template/demo_template.docx

代码部分

maven

		<!-- 用于生成word版报告 -->
		<dependency>
		    <groupid>com.deepoove</groupid>
		    <artifactid>poi-tl</artifactid>
		    <version>1.12.1</version>
		</dependency>

处理过程

	// 模板文件
    string templatefilepath = "templet/word_template/demo_template.docx";
    // 读取模板文件
    inputstream templatein = getclass().getclassloader().getresourceasstream(templatefilepath);

    // 插入文本数据
    map<string, object> templatedata = new hashmap<string, object>();
    templatedata.put("str1", "替换成功");

    // 生成模板文件
    xwpftemplate template = xwpftemplate.compile(templatein).render(templatedata);
    
    // 写入数据并关闭流
    template.writeandclose(new fileoutputstream("d:/output.docx"));
    templatein.close();

此时,文件便下载到了 d:/output.docx 的位置。

若为web

public void pgdexport(httpservletresponse response) throws ioexception {

        // 模板文件
        string templatefilepath = "templet/word_template/demo_template.docx";
        // 读取模板文件
        inputstream templatein = getclass().getclassloader().getresourceasstream(templatefilepath);

        // 插入文本数据
        map<string, object> templatedata = new hashmap<string, object>();
        templatedata.put("str1", "替换成功");

        // 生成模板文件
        xwpftemplate template = xwpftemplate.compile(templatein).render(templatedata);
        servletoutputstream outputstream = response.getoutputstream();

        // 防止文件以及文件名乱码
        string filename = "文件名.docx";
        string encodedfilename = urlencoder.encode(filename, "utf-8").replaceall("\\+", "%20");
        response.setcontenttype("application/vnd.openxmlformats-officedocument.wordprocessingml.document");
        response.setheader("content-disposition", "attachment; filename=\"" + encodedfilename + "\"; filename*=utf-8''" + encodedfilename);
        response.setcharacterencoding("utf-8");

			  // 写入数据并关闭流
        template.writeandclose(outputstream);
        templatein.close();
    }
// 前端js代码
// 如果此接口需要被鉴权,那么需要后端同时从params中获取token,然后前端拼接token传递,否则此方法无法使用
// 需要注意跨域问题
window.location.href =  '你的接口地址';

插入图片

模板:占位符格式是{{@xxxx}}

代码

import com.deepoove.poi.xwpftemplate;
import com.deepoove.poi.data.picturetype;
import com.deepoove.poi.data.pictures;

templatedata.put("img", pictures.ofstream(in, picturetype.jpeg).size(300, 300).create());

参考文章:java poi-tl根据模版生成word文件并插入文字、图片、表格、图表

到此这篇关于java poi-tl根据模板生成word文件的完整指南的文章就介绍到这了,更多相关java poi-tl模板生成word内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com