博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java Freemarker 根据模板生成Word
阅读量:2385 次
发布时间:2019-05-10

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

打开word写word 模板

word模板
将此模板另存为 xml
在这里插入图片描述
关闭word用notepad++打开精工发货通知单.xml格式化
在这里插入图片描述
搜索替换变量字段为${变量字段},图片部分base64值删掉也替换
在这里插入图片描述
替换后如下:
在这里插入图片描述
额外部分(懒得找遍历):
如果有表格需要遍历显示的,找到对应需要遍历的行,在外层用<#list ? as row ></#list>进行包围,遍历的行用 ${row.?}来替换,如下图所示:
在这里插入图片描述

将发货通知单.xml改成pfmb.ftl,并存放在项目templates下

在这里插入图片描述
引入freemarker依赖
在这里插入图片描述


在这里插入图片描述

书写WordUtil 工具类

import freemarker.template.Configuration;import freemarker.template.Template;import freemarker.template.TemplateException;import sun.misc.BASE64Encoder;import javax.servlet.http.HttpServletResponse;import java.io.*;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;public class WordUtil {
private Configuration configuration = null; public WordUtil() {
configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); } /** * 导出word 并提供下载 * @param response */ public void download(HttpServletResponse response) {
BufferedInputStream bis = null; BufferedOutputStream bos = null; try {
File file = createDoc(); String fileName = "发货通知单.doc"; response.setContentType("application/msword;charset=utf-8"); response.addHeader("Content-Disposition", "attachment; filename=\"" + new String(fileName.getBytes(),"iso-8859-1") + "\""); bis = new BufferedInputStream(new FileInputStream(file)); bos = new BufferedOutputStream(response.getOutputStream()); byte[] buff = new byte[10240]; int bytesRead; while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff, 0, bytesRead); } bis.close(); bos.close(); } catch (Exception e) {
e.printStackTrace(); } } /** * 将文本信息转为word输出 * @throws Exception */ public File createDoc() throws Exception {
Map
dataMap = new HashMap<>(); getData(dataMap); //创建数据 // configuration.setClassForTemplateLoading(this.getClass(), "templates"); //模板文件所在路径 configuration.setDirectoryForTemplateLoading(new File(WordUtil.class .getProtectionDomain().getCodeSource().getLocation().getPath() + "templates")); Template t = null; String fileName = "发货通知单.doc"; File file = new File(fileName); try {
t = configuration.getTemplate("pfmb.ftl"); //获取模板文件 t.setEncoding("utf-8"); Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName),"utf-8")); t.process(dataMap, out); //将填充数据填入模板文件并输出到目标文件 out.close(); } catch (IOException e) {
e.printStackTrace(); } catch (TemplateException e) {
e.printStackTrace(); } return file; } //获得数据 private void getData(Map
dataMap) {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:SS"); dataMap.put("djbh", "PF1000001"); dataMap.put("rq", sdf.format(new Date())); dataMap.put("khdm", "wh01"); dataMap.put("khmc", "武汉一号"); String image = getBase64("C:\\Users\\Minco\\Desktop\\photo\\1.jpg"); dataMap.put("img", image); } /*** * 处理图片 * @param watermarkPath 图片路径 * @return */ private String getBase64(String watermarkPath) {
InputStream in = null; byte[] data = null; try {
in = new FileInputStream(watermarkPath); data = new byte[in.available()]; in.read(data); in.close(); } catch (Exception e) {
e.printStackTrace(); } //对图片进行编码 BASE64Encoder encoder = new BASE64Encoder(); return encoder.encode(data); }}

控制层调用

@ResponseBody    @RequestMapping(value = "/download")    public  void downloadInfo(HttpServletResponse response)  throws Exception{
WordUtil wd = new WordUtil(); wd.download(response); }

完成,展示成果

在这里插入图片描述
小经验:
notepad++是插件化的工具,很多功能需要下载插件使用
复制文件到IDEAresources目录下,target里是不会自动去加载的,除非是clean重新发布,或者是CTRAL+F9加载资源文件
word模板制作的时候${}这个是不好先打上的,wordXML会把他们拆分开,就不符合freemarker的模板规范,所以只有在wordxml生成之后再进行替换

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

你可能感兴趣的文章
6 个 Linux性能监控命令行工具
查看>>
mysql 编码字符集配置
查看>>
php查看opcode编码的扩展 opdumper
查看>>
php转换html格式为文本格式
查看>>
mysql-proxy主从服务架构下读写分离和负载均衡实现及原理
查看>>
Nginx location 和 rewrite retry
查看>>
基于nginx的FastCGI的缓存配置
查看>>
Nginx模块fastcgi_cache的几个注意点
查看>>
PHP使用curl伪造IP地址和header信息
查看>>
代理服务器中的HTTP代理与SOCKS代理有什么区别?
查看>>
零基础Python学习路线,小白的进阶之路!
查看>>
CSS的23个垂直居中技巧,你都学会了吗?
查看>>
黑客攻击用的最短代码大揭秘,颠覆你的世界观!
查看>>
零基础的自学前端之路,当年的入坑之旅
查看>>
新手程序员?教你解决办法!基础都掌握了,动手敲代码就一脸懵逼
查看>>
程序员快速进阶学习到底要看书还是要看视频?
查看>>
web游戏框架哪家强?国内外精选优质框架分析及注意事项
查看>>
各行业都爱用什么编程语言开发?
查看>>
css3实现ps蒙版效果以及动画,炫酷吊炸天!
查看>>
程序员休息时间接私活遭公司辞退,不明觉厉?
查看>>