天天看点

使用freemarker导出word(含图片 表格动态行)

1、创建一个ftl文件

1.1创建一个word如下

使用freemarker导出word(含图片 表格动态行)

1.2将之保存到xml格式(word中的另存为)

使用freemarker导出word(含图片 表格动态行)

1.3 将eg10.xml文件的扩展名改为ftl

1.4 使用文本打开eg10.ftl文件

1.5 使用ctrl + F 查找aaaaa     在aaaaa前的遇到的第一个"<w:tr"之前    插入<#list newList as listKey>

使用freemarker导出word(含图片 表格动态行)

1.6 使用ctrl + F 查找cccccc     在cccccc后的遇到的第一个"</w:tr>"之后  插入</#list>

使用freemarker导出word(含图片 表格动态行)

1.7 使用ctrl + F 查找aaaaa  bbbbb ccccc 将之替换为  ${listKey.title}  ${listKey.content}   ${listKey.author}

1.8 更改图片的信息 

使用freemarker导出word(含图片 表格动态行)
使用freemarker导出word(含图片 表格动态行)

将上述图片两个开始到结束选中 替换 为 ${imgStr}

使用freemarker导出word(含图片 表格动态行)

这样ftl文件就修改好了

以下是代码

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import sun.misc.BASE64Encoder;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;

public class Test9 {

	/**
	 * 创建一个doc文件
	 * @param dataMap
	 * @param templateName
	 * @param filePath
	 * @param fileName
	 */
	public static void createWord(Map dataMap, String templateName,
			String filePath, String fileName) {
		//1.创建配置实例
		Configuration configuration = new Configuration();
		//2.设置编码
		configuration.setDefaultEncoding("UTF-8");
		//3.ftl模板同意放置到一个包中 src根目录
		configuration.setClassForTemplateLoading(Test9.class, "");
		//4.获取模板
		try {
			Template template = configuration.getTemplate(templateName);
			//5.创建输出文件 File对象
			File outFile = new File(filePath+""+fileName);
			//6.创建输出流
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(outFile),"UTF-8"));
			//7.生成文件
			template.process(dataMap, out);
			//8.关闭流
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (TemplateException e) {
			e.printStackTrace();
		}
	}
	
	public static String getImageStr() {
        String imgFile = "C:\\Users\\Administrator\\Pictures\\aa.jpg";
        InputStream in = null;
        byte[] data = null;
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(data);
    }

	public static void main(String[] args) {
		//用于组装word页面需要的数据
		Map<String, Object> dataMap = new HashMap<String, Object>();
		
		//组装数据
		dataMap.put("username", "张三");
		dataMap.put("sex", "男");
		List<Map<String, Object>> newList = new ArrayList<Map<String, Object>>();
		for(int i=1; i<=10; i++){
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("title", "标题"+i);
			map.put("content", "内容"+(i+1));
			map.put("author", "作者"+(i+1));
			newList.add(map);
		}
		dataMap.put("newList", newList);
		dataMap.put("imgStr", Test9.getImageStr());
		Test9.createWord(dataMap, "eg10.ftl", "", "out10.doc");
	}
}
           

架包为