博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java如何利用模板文件生成word文档
阅读量:1889 次
发布时间:2019-04-26

本文共 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文档的小程序。

转载地址:http://vezdf.baihongyu.com/

你可能感兴趣的文章
StringBuild的概述和详细使用方法以及案例练习
查看>>
Java面向对象总体知识结构思维导图(面授)
查看>>
服务器安装redis,并远程访问
查看>>
//输出100以内的素数
查看>>
阿里云ubantu64位系统安装JDK和Tomcat
查看>>
CentOS7_mysql数据库备份
查看>>
CentOS7_MySQL集群
查看>>
CentOS——zabbix部署
查看>>
jenkins——CentOS7基础搭建
查看>>
源码安装LAMP环境踩坑日志
查看>>
基础:LNMP环境搭建
查看>>
Nginx日常优化参数
查看>>
Tomcat虚拟主机基础配置
查看>>
Tomcat多实例基础配置
查看>>
LVS负载均衡(DR模式)集群配置
查看>>
keepalived+HAproxy高可用动静分离负载均衡
查看>>
Atlas代理MySQL读写分离
查看>>
为什么JavaScript是单线程?
查看>>
web代码合集js数据类型,强制转换,运算符,计算闰年, 实例化声明,构造函数声明,函数的嵌套,函数的自调用
查看>>
关于电脑开机出现,bootmgr is missing press ctrl+alt+del to restart解决问题
查看>>