java 使用freemarker生成word
来源:互联网 发布:华为it应用技术工程师 编辑:程序博客网 时间:2024/06/05 10:51
看了一些其他关于生成word的文档,不是展示的内容过多,就是代码示例不够详细,不能够满足初等技术人员的需求,本内容包括基本的数据填充和列表数据填充
1:创建word文档
2:另存为word2003 xml
3:填充变量
4:将xml文件修改后缀为ftl格式
发到项目包下
5:Java代码
package demo;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
/**
* 类说明:
*
* @version 2.0
* <p>
*
* <pre>
* 文件名: demo.java <br>
* 创建人: admin <br>
* 时间: 2015年7月24日<br>
* </pre>
*
* </p>
**/
public class demo {
private Configuration configuration = null;
public demo() {
configuration = new Configuration();
configuration.setDefaultEncoding("UTF-8");
}
public static void main(String[] args) {
demo dm = new demo();
dm.createWord();
}
public void createWord() {
Map<String, Object> dataMap = new HashMap<String, Object>();
getData(dataMap);
configuration.setClassForTemplateLoading(this.getClass(), "/demo"); // FTL文件所存在的位置
Template t = null;
try {
t = configuration.getTemplate("demo.ftl"); // 文件名
} catch (IOException e) {
e.printStackTrace();
}
File outFile = new File("E:/demo.doc");
Writer out = null;
try {
out = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(outFile)));
} catch (FileNotFoundException e1) {
e1.printStackTrace();
}
try {
t.process(dataMap, out);
} catch (TemplateException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void getData(Map<String, Object> dataMap) {
dataMap.put("Title", "文档标题");
dataMap.put("date",
new SimpleDateFormat("yyyy-MM-dd").format(new Date()));
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
for (int i = 0; i < 10; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("Number", i);
map.put("Content", "文本内容" + i);
list.add(map);
}
dataMap.put("datelist", list);
}
}
6:Java包结构
7:生成
- java 使用freemarker生成word
- Java使用freeMarker生成Word
- 利用Freemarker生成word的使用(java)
- java 使用freemarker 生成word模板
- java使用 freemarker 模板生成word文件
- java生成word之freemarker使用
- 关于Freemarker生成word的使用 java生成word
- 使用Freemarker生成Word
- 使用freemarker生成word
- 使用freemarker生成word
- 使用freemarker生成word
- Java Freemarker生成word
- java实现word套打(关于Freemarker生成word的使用(java生成word))
- 使用FreeMarker生成word文档
- 使用FreeMarker生成Word文档
- java 生成word应用freemarker
- Java Web项目中使用Freemarker生成Word文档
- Java Web项目中使用Freemarker模版生成Word文档
- [LeetCode]Search a 2D Matrix II
- I2C总线和SPI总线
- mybatis 实现关联查询的一对多和多对多查询
- 方法内部lambda表达式中若用到变量,若在方法内lambda外,则只能为final,或者定义为全局变量
- 好压报错“启动参数错误,程序即将退出!”
- java 使用freemarker生成word
- Num 12: HDOJ: 题目1004 : Let the Balloon Rise( 字符串问题 )
- addChildViewController
- Redis 配置文件说明
- 如何用textview设置drawableLeft属性
- storm基础框架分析
- Lowest Common Ancestor of a Binary Tree
- UVa 11538 Chess Queen 象棋中的皇后
- HDU 5308 I Wanna Become A 24-Point Master