java如何利用模板文件生成word文档
发布日期:2021-06-29 15:52:07 浏览次数:4 分类:技术文章

本文共 3658 字,大约阅读时间需要 12 分钟。

java如何利用模板文件生成word文档

在这里插入图片描述

使用java语言根据不同的数据动态生成word文档流程比较简单。需要的技能

  • 能够看懂xml文件
  • 会添加项目依赖的jar包(这里会用到freemarker包)

整体的流程是这样的:

  1. 制作模板文档
  2. 程序中读取文件和数据,利用数据替换模板中的标签
  3. 输出文档

word模板文档

word文档制作

  • 先要制作一份完整的文档,记住文档中需要替换的部分

  • 将文档在word软件中另存为Word XML 文档(*.xml)

  • 使用任意文本编辑器打开刚才保存的xml文件(切记不要使用word软件打开xml文档)

  • 利用${<name>}标记需要替换的部分

    • 这里的name可以自定义,程序中会使用map的key来匹配
    • 单个属性直接使用${<name>}
    • 对于表格这样需要重复的,需要将<w:tr>标签使用<#list>包起来,同时定义变量的循环变量(例如<#list users as user>),然后在标签里面使用循环变量调用(例如:${user.name}

示例:

时间:${date}
<#list orders as order>         
${order.name}
  • 对于图片,xml文档中会将图片转码为Base64的字符串,制作模板文档是,需要将这个字符串换成${<name>},按照单个变量来处理

freemarker介绍

(这部分介绍使用的第三方工具,可以跳过,不影响技术学习)

FreeMarker是一款模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页、电子邮件、配置文件、源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。

官网介绍如下

Apache FreeMarker™ is a template engine: a Java library to generate text output (HTML web pages, e-mails, configuration files, source code, etc.) based on templates and changing data. Templates are written in the FreeMarker Template Language (FTL), which is a simple, specialized language (not a full-blown programming language like PHP). Usually, a general-purpose programming language (like Java) is used to prepare the data (issue database queries, do business calculations). Then, Apache FreeMarker displays that prepared data using templates. In the template you are focusing on how to present the data, and outside the template you are focusing on what data to present.

在这里插入图片描述

程序

pom依赖

org.freemarker
freemarker
2.3.30

主程序

public static void main(String[] args) {
//这个map存储模板文档中需要替换的数据 Map
map=new HashMap<>(); map.put("name","zhangsan"); List
orders=new ArrayList<>(); Order order1=new Order(); order1.setName("手机"); order1.setCount(1); order1.setPrice(4561.22); order1.setUnit("部"); order1.setNote("xiaomi 13"); Order order2=new Order(); order2.setName("手机2"); order2.setCount(23); order2.setPrice(2000.88); order2.setUnit("部"); order2.setNote("huawei Mate10"); orders.add(order1); orders.add(order2); map.put("orders",orders); map.put("date", DateUtil.getDateStr()); //这里将图片转换成字符串添加到map中 map.put("img1", ImageUtil.encodingImg("data/imgs/1.jpg")); try {
Configuration configuration = new Configuration(new Version("2.3.30")); configuration.setDefaultEncoding("utf-8"); configuration.setDirectoryForTemplateLoading(new File("data/template/")); //最后输出的文档 //这里最好是 .doc File outFile=new File("data/result.doc"); Template template=configuration.getTemplate("template.xml"); Writer out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile))); template.process(map,out); out.flush(); out.close(); System.out.println("输出成功"); }catch (Exception e){
e.printStackTrace(); }}

图片转字符串

public static String encodingImg(String file){
InputStream in = null; byte[] data = null; try {
in = new FileInputStream(file); data = new byte[in.available()]; in.read(data); in.close(); } catch (Exception e) {
e.printStackTrace(); } BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data);}

结束

至此,我们完成了一个简单的生成word文档的小程序。

转载地址:https://console.blog.csdn.net/article/details/109625043 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:java读写xlsx格式的MS Excel文件
下一篇:Java如何读写注册表

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年04月16日 14时48分41秒