freemarker生成标准格式word

1. 导入jar包:
import sun.misc.BASE64Encoder;//用于插入图片
import freemarker.template.Configuration;
import freemarker.template.Template;

2.初始化生成doc所需的类
/**
* 初始化生成doc所需的类
*/
public ReportServiceImpl() {
     configuration = new Configuration();
     configuration.setDefaultEncoding("UTF-8");
}
3.准备数据
// 要填入模本的数据文件
Map<String, Object> dataMap = new HashMap<String, Object>();
dataMap.put("testName", testNameData);//封装动态生成的数据
4.根据文档模版生成流
// 读取文档模板
configuration.setClassForTemplateLoading(this.getClass(), "../template/document");
Template t = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Writer out = null;
t = configuration.getTemplate("downLoad_" + reportInfo.getFormatId() + "_template.ftl");
out = new BufferedWriter(new OutputStreamWriter(baos, "UTF-8"));
t.process(dataMap, out);
data = baos.toByteArray();// 得到最后的字节流

猜你喜欢

转载自bluesun-bluesun.iteye.com/blog/1675034
今日推荐