1.模板
username:${username}
password:${password}
<#list mylist as item>
${item.name!}
${item.password!}
${item.sex!}
${item.phto!}
</#list >
2.Java类
package servlet; import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
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 org.junit.Test; import bean.TestFreemarkerBean;
import freemarker.template.Configuration;
import freemarker.template.Template; public class FreeMarkerExportWord { @Test
public void exportSimpleWord() throws Exception {
// 要填充的数据, 注意map的key要和word中${xxx}的xxx一致
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("username", "张三");
dataMap.put("password", "男"); // Configuration用于读取ftl文件
Configuration configuration = new Configuration();
configuration.setDefaultEncoding("utf-8"); /*
* 以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是 指定ftl文件所在目录的路径,而不是ftl文件的路径
*/
// 指定路径的第一种方式(根据某个类的相对路径指定)
configuration.setClassForTemplateLoading(this.getClass(), "");
// 指定路径的第二种方式,我的路径是C:/a.ftl
// configuration.setDirectoryForTemplateLoading(new File("C:/")); List<TestFreemarkerBean> list = new ArrayList<>();
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
list.add(new TestFreemarkerBean("乔治", "123456", "男", "wwwwwwww"));
dataMap.put("mylist", list);
// 输出文档路径及名称
File outFile = new File("F:/test.doc"); // 以utf-8的编码读取ftl文件
Template t = configuration.getTemplate("test2.ftl", "utf-8");
Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240);
t.process(dataMap, out);
out.close();
}
}
3.测试:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAQEAAAFwCAIAAAA38FnOAAAYI0lEQVR4nO2dwZmjvBJFlQIxKAVyUAiKgQT+hVazJQMyIIGnCEhACZCBcuAt7teaGoGx3T2Inq57VmOwsdxTB0oClcxGiG7M3Q0g5GboANEOHSDaoQNEO3SAaIcOEO3QAaIdOkC0QweIdugA0Q4dINqhA0Q7dIBohw4Q7dABoh06QLRDB4h2rnLAGNpF/g3oANEOHSDaoQNEO3SgNeu6ln8755ZlubExZKMDDYgxjoK+78dxxC5jjFRi27ac8+e+ZRzHGONX26oSOnA5OedH5/7qr7QsS9/3lQbTNI3PCCEYY7quSyk1+EU/DDrQmkcO5Jy7rvPeV1eG6iX569CB1hw6kHPu+36e5/vapRc60ALvfclbrLWVAzlndo5vhA785utR2Pd913X7fq0M8RBCSW/QJx6GgQnPjVwVqf/9958xxjn3+5uMMcbIOIsx9n2P7d77EII8QtlrrS0DKdu2jePovcfZNOc8DAPes67ruq4hBGtt2d51nUwwcs54A750GAbE67IsaC06l977lJK1tus6OdjyqEmFVxyQGGOmaao2vqtizpkjQl/hwuvAOI7SgZyzdGBd15IV4Fwo3xxj7Lqu7JXjicgocCjv/bIsKSVjTEoJx0EQ47MxRjn+6L0vnU5kIMMwlMPiG9HOEAKE8d4/bdJTuq6Tf4cy1IO/ksR7b4ypOgbnQ0PQ8rwv8evXrxebqpB2DmBjcWBZlmqvfGmtlee2lJIMIxxqP4SyfZzR5ZlYfmkIQcaKbKH8d/mI3Pi0SY+Ah2UQs5i2fWSM+0tBxbqun75vAOjACbc5gJGQEEKMEf/BJaBxXt8fUCYJj858cODkg3hPSahedOCVJj0i51yaWpmPY8YYrxgRknFPB064zYHtIytADl1S8+0jjvdUDhzG37kDyP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