标签:style blog http io ar color os 使用 sp
1、 制作模板
首先用office【版本要2003以上,以下的不支持xml格式】编辑文档的样式,将需要动态填充的内容使用Freemarker标签替换:Word文档样式如下:
将Word文档另存为XML格式,将后缀名“xml”修改为“ftl”,模板制作完成
图片处理:
我们可以看到上面的模板中有图片,当我们将xml修改为ftl之后打开文件可以看到原来图片的位置全部都是乱码。这是因为ftl不能够识别图片,我们只要将这些乱码用${image}替换掉既可以了。

循环处理:
模板中明细表中的数据是一个集合,我们需要循环遍历才能够将数据全部注入,这个时候我们就会用到了FreeMarker本身的语法了。
循环:
<#list array as bean>
${bean.property}
</#list>
代码放在表格的开头和结尾,也即是将表格中的标签包括在循环里面,这样子就可以了。
在制作模板的过程中遇到的一个问题:${xxx}这些标记在ftl中被拆成一个一个的,让我很无语。最后我还是手动的将这些又重新拼到一起了。
2. 准备数据,将上面模板中的${}的标签放上对应的数据
首先要讲freemarker的jar包放到程序中,然后将上面制作好的模板放到程序中;

最后加上下面的代码就可以了
- package com.ftl;
-
- import java.io.*;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import freemarker.template.Configuration;
- import freemarker.template.Template;
- import freemarker.template.TemplateException;
- import sun.misc.BASE64Encoder;
-
- public class DocumentHandler {
-
- private Configuration configuration = null;
-
- public DocumentHandler() {
- configuration = new Configuration();
- configuration.setDefaultEncoding("utf-8");
- }
-
- public void createDoc() {
-
- Map dataMap = new HashMap();
- getData(dataMap);
-
-
- configuration.setClassForTemplateLoading(this.getClass(),
- "/com/template");
- Template t = null;
- try {
-
- t = configuration.getTemplate("temple.ftl");
- t.setEncoding("utf-8");
-
- } catch (IOException e) {
- e.printStackTrace();
- }
-
- File outFile = new File("D:/test.doc");
- Writer out = null;
-
- try {
- out = new BufferedWriter(new OutputStreamWriter(
- new FileOutputStream(outFile), "utf-8"));
- } catch (Exception e1) {
- e1.printStackTrace();
- }
-
- try {
- t.process(dataMap, out);
- out.close();
- } catch (TemplateException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
-
-
- @SuppressWarnings("unchecked")
- private void getData(Map dataMap) {
- dataMap.put("image", getImageStr());
- dataMap.put("name", "张三");
-
- List<Map<String, Object>> newsList=new ArrayList<Map<String,Object>>();
- for(int i=1;i<=5;i++){
- Map<String, Object> map=new HashMap<String, Object>();
- map.put("purchaseTime", "进货日期"+i);
- map.put("product", "产品名称"+i);
- map.put("factory", "生产厂家"+i);
- map.put("spec", "产品规格"+i));
- map.put("number", "进货数量"+i);
- newsList.add(map);
- }
- dataMap.put("newsList",newsList);
- }
- private String getImageStr() {
- String imgFile = "d:/1.png";
- InputStream in = null;
- byte[] data = null;
- try {
- in = new FileInputStream(imgFile);
- data = new byte[in.available()];
- in.read(data);
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- BASE64Encoder encoder = new BASE64Encoder();
- return encoder.encode(data);
- }
-
- }
- package com.ftl;
-
- public class main {
-
- public static void main(String[] args) {
- DocumentHandler dh=new DocumentHandler();
- dh.createDoc();
- System.out.println("end");
- }
-
- }
FreeMarker之生成复杂word(二)
标签:style blog http io ar color os 使用 sp
原文地址:http://www.cnblogs.com/yuyanbian/p/4132286.html