前文我们介绍了通过Apache POI通过来导出word的例子;那如果是word模板方式,有没有开源库通过模板方式导出word呢?poi-tl是一个基于Apache POI的Word模板引擎,也是一个免费开源的Java类库,你可以非常方便的加入到你的项目中,并且拥有着让人喜悦的特性。本文主要介绍通过SpringBoot集成poi-tl实现模板方式的Word导出功能。

  • SpringBoot集成文件 - 集成POI-tl之基于模板的Word导出

    • 知识准备

      • 什么是poi-tl
      • poi-tl的TDO模式
        • Template:模板
        • Data-model:数据
        • Output:输出
    • 实现案例
      • Pom依赖
      • 导出基于template的word
      • 导出markdown为word
    • 示例源码
    • 参考文章
    • 更多内容

知识准备

需要理解文件上传和下载的常见场景和技术手段。@pdai

什么是poi-tl

如下内容来源于,poi-tl官网。

poi-tl(poi template language)是Word模板引擎,使用Word模板和数据创建很棒的Word文档。

优势:

它还支持自定义插件,如下是官网代码仓库支持的特性

poi-tl supports custom functions (plug-ins), functions can be executed anywhere in the Word template, do anything anywhere in the document is the goal of poi-tl.

Feature Description
✅ Text Render the tag as text
✅ Picture Render the tag as a picture
✅ Table Render the tag as a table
✅ Numbering Render the tag as a numbering
✅ Chart Bar chart (3D bar chart), column chart (3D column chart), area chart (3D area chart), line chart (3D line chart), radar chart, pie chart (3D pie Figure) and other chart rendering
✅ If Condition Hide or display certain document content (including text, paragraphs, pictures, tables, lists, charts, etc.) according to conditions
✅ Foreach Loop Loop through certain document content (including text, paragraphs, pictures, tables, lists, charts, etc.) according to the collection
✅ Loop table row Loop to copy a row of the rendered table
✅ Loop table column Loop copy and render a column of the table
✅ Loop ordered list Support the loop of ordered list, and support multi-level list at the same time
✅ Highlight code Word highlighting of code blocks, supporting 26 languages ​​and hundreds of coloring styles
✅ Markdown Convert Markdown to a word document
✅ Word attachment Insert attachment in Word
✅ Word Comments Complete support comment, create comment, modify comment, etc.
✅ Word SDT Complete support structured document tag
✅ Textbox Tag support in text box
✅ Picture replacement Replace the original picture with another picture
✅ bookmarks, anchors, hyperlinks Support setting bookmarks, anchors and hyperlinks in documents
✅ Expression Language Fully supports SpringEL expressions and can extend more expressions: OGNL, MVEL…
✅ Style The template is the style, and the code can also set the style
✅ Template nesting The template contains sub-templates, and the sub-templates then contain sub-templates
✅ Merge Word merge Merge, you can also merge in the specified position
✅ custom functions (plug-ins) Plug-in design, execute function anywhere in the document

poi-tl的TDO模式

TDO模式:Template + data-model = output

以官网的例子为例:

XWPFTemplate template = XWPFTemplate.compile("template.docx").render(new HashMap<String, Object>(){{put("title", "Hi, poi-tl Word模板引擎");
}});
template.writeAndClose(new FileOutputStream("output.docx"));
  • compile 编译模板 - Template
  • render 渲染数据 - data-model
  • write 输出到流 - output

Template:模板

模板是Docx格式的Word文档,你可以使用Microsoft office、WPS Office、Pages等任何你喜欢的软件制作模板,也可以使用Apache POI代码来生成模板。

所有的标签都是以{{开头,以}}结尾,标签可以出现在任何位置,包括页眉,页脚,表格内部,文本框等,表格布局可以设计出很多优秀专业的文档,推荐使用表格布局。

poi-tl模板遵循“所见即所得”的设计,模板和标签的样式会被完全保留。

Data-model:数据

数据类似于哈希或者字典,可以是Map结构(key是标签名称):

Map<String, Object> data = new HashMap<>();
data.put("name", "Sayi");
data.put("start_time", "2019-08-04");

可以是对象(属性名是标签名称):

public class Data {private String name;private String startTime;private Author author;
}

数据可以是树结构,每级之间用点来分隔开,比如{ {author.name} }标签对应的数据是author对象的name属性值。

Word模板不是由简单的文本表示,所以在渲染图片、表格等元素时提供了数据模型,它们都实现了接口RenderData,比如图片数据模型PictureRenderData包含图片路径、宽、高三个属性。

Output:输出

以流的方式进行输出:

template.write(OutputStream stream);

可以写到任意输出流中,比如文件流:

template.write(new FileOutputStream("output.docx"));

比如网络流:

response.setContentType("application/octet-stream");
response.setHeader("Content-disposition","attachment;filename=\""+"out_template.docx"+"\"");// HttpServletResponse response
OutputStream out = response.getOutputStream();
BufferedOutputStream bos = new BufferedOutputStream(out);
template.write(bos);
bos.flush();
out.flush();
PoitlIOUtils.closeQuietlyMulti(template, bos, out); // 最后不要忘记关闭这些流。

实现案例

这里展示SpringBoot集成poi-tl基于word模板导出Word, 以及导出markdown为word的例子。

Pom依赖

引入poi的依赖包

基础的包:

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

插件的包如下,比如highlight,markdown包

<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl-plugin-highlight</artifactId><version>1.0.0</version>
</dependency>
<dependency><groupId>com.deepoove</groupId><artifactId>poi-tl-plugin-markdown</artifactId><version>1.0.3</version>
</dependency>

导出基于template的word

controller中的方法

@ApiOperation("Download Word")
@GetMapping("/word/download")
public void download(HttpServletResponse response) {try {XWPFTemplate document = userService.generateWordXWPFTemplate();response.reset();response.setContentType("application/octet-stream");response.setHeader("Content-disposition","attachment;filename=user_word_" + System.currentTimeMillis() + ".docx");OutputStream os = response.getOutputStream();document.write(os);os.close();} catch (Exception e) {e.printStackTrace();}
}

Service中的实际方法

@Override
public XWPFTemplate generateWordXWPFTemplate() throws IOException {Map<String, Object> content = new HashMap<>();content.put("title", "Java 全栈知识体系");content.put("author", "pdai");content.put("site", new HyperlinkTextRenderData("https://pdai.tech", "https://pdai.tech"));content.put("poiText", "Apache POI 是创建和维护操作各种符合Office Open XML(OOXML)标准和微软的OLE 2复合文档格式(OLE2)的Java API。用它可以使用Java读取和创建,修改MS Excel文件.而且,还可以使用Java读取和创建MS Word和MSPowerPoint文件。更多请参考[官方文档](https://poi.apache.org/index.html)");content.put("poiText2", "生成xls和xlsx有什么区别?POI对Excel中的对象的封装对应关系?");content.put("poiList", Numberings.create("excel03只能打开xls格式,无法直接打开xlsx格式","xls只有65536行、256列; xlsx可以有1048576行、16384列","xls占用空间大, xlsx占用空间小,运算速度也会快一点"));RowRenderData headRow = Rows.of("ID", "Name", "Email", "TEL", "Description").textColor("FFFFFF").bgColor("4472C4").center().create();TableRenderData table = Tables.create(headRow);getUserList().forEach(a -> table.addRow(Rows.create(a.getId() + "", a.getUserName(), a.getEmail(), a.getPhoneNumber() + "", a.getDescription())));content.put("poiTable", table);Resource resource = new ClassPathResource("pdai-guli.png");content.put("poiImage", Pictures.ofStream(new FileInputStream(resource.getFile())).create());return XWPFTemplate.compile(new ClassPathResource("poi-tl-template.docx").getFile()).render(content);
}private List<User> getUserList() {List<User> userList = new ArrayList<>();for (int i = 0; i < 5; i++) {userList.add(User.builder().id(Long.parseLong(i + "")).userName("pdai" + i).email("pdai@pdai.tech" + i).phoneNumber(121231231231L).description("hello world" + i).build());}return userList;
}

准备模板

导出word

导出markdown为word

controller中的方法

@ApiOperation("Download Word based on markdown")
@GetMapping("/word/downloadMD")
public void downloadMD(HttpServletResponse response) {try {XWPFTemplate document = userService.generateWordXWPFTemplateMD();response.reset();response.setContentType("application/octet-stream");response.setHeader("Content-disposition","attachment;filename=user_word_" + System.currentTimeMillis() + ".docx");OutputStream os = response.getOutputStream();document.write(os);os.close();} catch (Exception e) {e.printStackTrace();}
}

Service中实现的方法


@Override
public XWPFTemplate generateWordXWPFTemplateMD() throws IOException {MarkdownRenderData code = new MarkdownRenderData();Resource resource = new ClassPathResource("test.md");code.setMarkdown(new String(Files.readAllBytes(resource.getFile().toPath())));code.setStyle(MarkdownStyle.newStyle());Map<String, Object> data = new HashMap<>();data.put("md", code);Configure config = Configure.builder().bind("md", new MarkdownRenderPolicy()).build();return XWPFTemplate.compile(new ClassPathResource("markdown_template.docx").getFile(), config).render(data);
}

准备模板

导出word

示例源码

https://github.com/realpdai/tech-pdai-spring-demos

参考文章

http://deepoove.com/poi-tl/

更多内容

告别碎片化学习,无套路一站式体系化学习后端开发: Java 全栈知识体系(https://pdai.tech)

SpringBoot集成文件 - 如何基于POI-tl和word模板导出庞大的Word文件?相关推荐

  1. 基于Easypoi+jfree,使用SpringBoot架构,Java编程实现word模板导出Word报表

    目录 1.项目目录结构 2.pom.xml添加的依赖 3.编写jfreeutil工具类 4.编写wordutil工具类 5.编写word模板 7.运行效果 8.复杂布局实现 8.1如何实现图片并排 S ...

  2. POI之excel固定模板导出

    POI之excel固定模板导出 一.简介 二.excel模板 三.项目中maven依赖 四.Excel模板操作代码 五.Controller层excel模板导出接口代码 六.导出excel 一.简介 ...

  3. excel每行按模板导出为一个excel文件,可以指定列文本生成二维码或者条形码

    程序修改了bug,增加了功能.(20220825) 该程序可以把一个excel文件中每行数据按指定模板生成一个单独excel文件. 1. 模板文件为tpl\template.xlsx. 2. 从左侧数 ...

  4. SpringBoot+EasyPOI word模板导出,含多张图片

    这几天客户提出了新的需求,要求记录要能够导出word,并且里面包含的图片也要导出来,这里借用EasyPOI来进行操作. 参考文章:https://blog.csdn.net/qq_34752942/a ...

  5. springboot整合poi(使用EXCEL模板导出导入)

    springboot整合poi 依赖 <!-- poi依赖--><dependency><groupId>org.apache.poi</groupId> ...

  6. freemarker基于docx格式创建模板导出带图片pdf文件

    目录 一.实现思路: 二.基于docx格式文件创建模板: (1)替换数据占位符: (2)将替换数据占位符文件强制修改zip格式: (3)获取文本数据文件document.xml: (4)获取图片依赖文 ...

  7. Springboot--使用POI,根据word模板导出word文件

    需求:根据一个word模板,在程序中替换模板中的参数,然后根据这个模板导出word文件. 引入POI对word操作的依赖: <dependency><groupId>org.a ...

  8. poi tl 判断空值_使用poi-tl操作word模板

    使用poi-tl操作word模板 1.导入jar包支持 com.deepoove poi-tl 1.8.2 2.建立word模板 在电脑E盘中建立word模板:"E:\templete.do ...

  9. vue项目导出word文件(根据word模板导出)

    一.安装依赖包 1.docxtemplater npm install docxtemplater pizzip -S 2.jszip-utils npm install jszip-utils -S ...

最新文章

  1. 存储能否导致ESXi网络性能问题?
  2. 学python必须知道的30个技巧
  3. 第一章 初识Linux shell
  4. 处理SAP Netweaver gateway service使用过程中遇到的404 error
  5. windows java配置_菜鸟教程 windows 配置java的环境变量
  6. Python函数的可变参数传递(*args与**kwargs)
  7. Nginx+PHP-FPM优化技巧总结(转发别人的,自己留着收藏个记录用)
  8. 对警报线程池的警报线程_如何建立更好的警报
  9. 王者荣耀女性机器人面世;深圳中院受理金立破产案;Firefox 64 发布 | 极客头条...
  10. Java使用文本编写源代码
  11. 串口波形显示软件SerialChart的使用
  12. 互联网行业定制网站需要多少钱
  13. Nyko推出平板手柄 为运行在Tegra3上的游戏特别打造
  14. 华为初面+综合面试(技术面)
  15. vue-seamless-scroll 无缝滚动 使用方法
  16. Camera ITS当中的test_ev_compensation_basic测试
  17. 关于串口波特率的的记录
  18. 场景建造软件_有效地构建。 建造者关于有目的地交付软件的观点
  19. matlab生成满足二维高斯(正…
  20. python人工智能项目实例-python人工智能项目实战,PDF+源码

热门文章

  1. 《阿里巴巴 Java开发手册》读后感
  2. 阿里云ECS云服务器Linux Tomcat启动慢 访问网页转圈
  3. 开发微信小程序的必备技能图谱
  4. 关于北美信号T1和欧洲信号E1的计算
  5. 生活,令人满意的生活,丰富的生活包括了起起落落,包括了痛苦和再次振作,包括了失败和再次奋
  6. 葵花宝典第一招:唐氏均线成交量参数
  7. iOS 三方app读取苹果健康数据
  8. 链接如何生成二维码?怎样创建一个网址二维码?
  9. SunOne中的domian常用命令
  10. AE开发之图层渲染20210603