Java之利用FreeMarker导出Word实例
来源:互联网 发布:手机qq java精简版 编辑:程序博客网 时间:2024/06/05 14:44
开心一笑
感冒了很难受,她闷在被窝里给男朋友发短信"我感冒了…"并决定如果对方回答"多喝点水"就一脚踹了他。过一会儿手机振动起来,短信内容:"开门。"……这个大笨蛋!谁让他来的啦!她起身用最快的速度冲去门口,此时手机再次振动,她一手开门一手兴奋的点开:"多呼吸点新鲜空气,运动运动。
提出问题
Java中如何利用FreeMarker导出word文档???
简书地址:http://www.jianshu.com/users/d38a3668be58/latest_articles
解决问题
1.先用word准备一个模板,如下图:
2.我们把word文档另存为xml格式的文件,用Notepad++工具打开,一下只截取部分内容.
3.我们开始处理第一个问题:文字处理,Ctrl+F找到XML文档中文字,将“以下省略一万字”替换为${textDeal},保存文件,将文件的后缀改为.ftl,自此模板制作成功。
4.编码实现文本替换:
package com.hwy.test;import freemarker.template.Configuration;import freemarker.template.Template;import java.io.*;import java.util.HashMap;import java.util.Map;/** * word导出 * Created by Ay on 2016/6/27. */public class WordDocExportTest { public static void main(String[] args) throws Exception{ /** 初始化配置文件 **/ Configuration configuration = new Configuration(); /** 设置编码 **/ configuration.setDefaultEncoding("utf-8"); /** 我的ftl文件是放在D盘的**/ String fileDirectory = "D:\\"; /** 加载文件 **/ configuration.setDirectoryForTemplateLoading(new File(fileDirectory)); /** 加载模板 **/ Template template = configuration.getTemplate("FreeMarker中word导出XML.ftl"); /** 准备数据 **/ Map<String,String> dataMap = new HashMap<>(); /** 在ftl文件中有${textDeal}这个标签**/ dataMap.put("textDeal","一下省略一万字"); /** 指定输出word文件的路径 **/ String outFilePath = "D:\\myFreeMarker.doc"; File docFile = new File(outFilePath); FileOutputStream fos = new FileOutputStream(docFile); Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240); template.process(dataMap,out); if(out != null){ out.close(); } }}
5.编码实现图片的替换
具体代码如下:
package com.hwy.test;import freemarker.template.Configuration;import freemarker.template.Template;import sun.misc.BASE64Encoder;import java.io.*;import java.util.HashMap;import java.util.Map;/** * word导出 * Created by Ay on 2016/6/27. */public class WordDocExportTest { public static void main(String[] args) throws Exception{ /** 初始化配置文件 **/ Configuration configuration = new Configuration(); /** 设置编码 **/ configuration.setDefaultEncoding("utf-8"); /** 我的ftl文件是放在D盘的**/ String fileDirectory = "D:\\"; /** 加载文件 **/ configuration.setDirectoryForTemplateLoading(new File(fileDirectory)); /** 加载模板 **/ Template template = configuration.getTemplate("FreeMarker中word导出XML.ftl"); /** 准备数据 **/ Map<String,String> dataMap = new HashMap<>(); /** 图片路径 **/ String imagePath = "D:\\apple.jpg"; /** 将图片转化为**/ InputStream in = null; byte[] data = null; try { in = new FileInputStream(imagePath); data = new byte[in.available()]; in.read(data); in.close(); } catch (Exception e) { e.printStackTrace(); }finally { if(in != null){ in.close(); } } /** 进行base64位编码 **/ BASE64Encoder encoder = new BASE64Encoder(); /** 在ftl文件中有${textDeal}这个标签**/ dataMap.put("textDeal","一下省略一万字"); /** 图片数据**/ dataMap.put("myImage",encoder.encode(data)); /** 指定输出word文件的路径 **/ String outFilePath = "D:\\myFreeMarker.doc"; File docFile = new File(outFilePath); FileOutputStream fos = new FileOutputStream(docFile); Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240); template.process(dataMap,out); if(out != null){ out.close(); } }}
结果,I love you这张图片又出现了:
6.freeMarker表格处理
1)同样道理,找到表格的位置,替换成freeMarker标签,图片如下:
具体代码:
package com.hwy.test;import freemarker.template.Configuration;import freemarker.template.Template; import java.io.*;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;/** * word导出 * Created by Ay on 2016/6/27. */public class WordDocExportTest { public static void main(String[] args) throws Exception{ /** 初始化配置文件 **/ Configuration configuration = new Configuration(); /** 设置编码 **/ configuration.setDefaultEncoding("utf-8"); /** 我的ftl文件是放在D盘的**/ String fileDirectory = "D:\\"; /** 加载文件 **/ configuration.setDirectoryForTemplateLoading(new File(fileDirectory)); /** 加载模板 **/ Template template = configuration.getTemplate("FreeMarker中word导出XML.ftl"); /** 准备数据 **/ Map<String,List<Student>> dataMap = new HashMap<>(); /** 表格数据初始化 **/ List<Student> studentList = new ArrayList<>(); studentList.add(new Student("100424060","小毅","男","25")); studentList.add(new Student("100424030","小兰","女","25")); /** 表格数据 studentList和freemarker标签要对应**/ dataMap.put("studentList",studentList); /** 指定输出word文件的路径 **/ String outFilePath = "D:\\myFreeMarker.doc"; File docFile = new File(outFilePath); FileOutputStream fos = new FileOutputStream(docFile); Writer out = new BufferedWriter(new OutputStreamWriter(fos, "utf-8"),10240); template.process(dataMap,out); if(out != null){ out.close(); } }}
Student类如下:这里有一个问题,如果Student类写在WordDocExportTest类中,就会出现问题,很奇怪,只能单独另启一个类了。
package com.hwy.test;/** * Created by Ay on 2016/6/29. */public class Student{ private String id; private String name; private String sex; private String age; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public Student(String id, String name, String sex, String age) { this.id = id; this.name = name; this.sex = sex; this.age = age; }}
结果如下:
![这里写图片描述](http://img.blog.csdn.net/20160629135612181)
7.word中字体样式调整
1)我们把四种的字体“这里是字体样式……”在word中字体样式改为仿宋_gb2312,大小改为30号如下:
2)用同样的方法,把word转变为ftl文件,打开如下
所有以后word需要什么的样式,都是在原先word调好样式,在转变为ftl文件即可
8.页眉页脚设置
1)相同道理,在原先word插入页眉“Hello Freemarker”,另存为xml文件,再后缀为ftl文件即可,既是我们要的文件
读书感悟
来自《我脑海中的橡皮擦》
- 我们破产了!没关系,我来付钱。
- 不要困扰于过去,时间会治愈一切。
- 要想原谅一个人,就是要把心里的某个地方留给他。
其他
如果有带给你一丝丝小快乐,就让快乐继续传递下去,欢迎转载,点赞,顶,欢迎留下宝贵的意见,多谢支持!
- Java之利用FreeMarker导出Word实例
- java导出word之freemarker导出
- java导出word之freemarker导出
- JAVA FreeMarker导出word
- java导出word(Freemarker)
- java web项目利用freemarker导出word文件小例子
- JAVA利用FreeMarker生成(导出)Word文档
- freemarker利用word模版导出word文档
- java导出word之freemarker导出(详细教程)
- java导出word之freemarker导出(详细教程)
- JAVA Freemarker 导出word文档实例带有图片
- Java用freemarker导出word
- Java用freemarker导出word
- Java用freemarker导出word
- Java用freemarker导出word
- Java用freemarker导出word
- Java用freemarker导出word
- java用freemarker导出word
- c++函数重载
- javascript学习2——基础
- Leetcode题解 111. Minimum Depth of Binary Tree
- 剑指offer-最小的K个数
- samba配置失败
- Java之利用FreeMarker导出Word实例
- FINDSTR 命令使用详解
- hdu_3804_Query on a tree(树链剖分)
- 《Java JD7 学习笔记》课后练习题3
- “爱运动管理系统”诞生记(3)
- liunux cenos 6.5 彻底卸载MySQL和安装MySQL
- hdu 2222 Keywords Search
- XML---DTD
- Windows Java EE 7 基本环境配置 及 错误解决