JAVA FreeMarker导出word

来源:互联网 发布:新浪数据图表 编辑:程序博客网 时间:2024/05/17 02:09

编辑word模板(Template)文件

1.word中新建一文档,设计好要导出的模板样式。其中添加参数: ${export}
如:


2、将处理好的的word模板,另存为xml格式或.ftl格式的文档。

FreeMaker使用Template文件导出word

1.设置参数
configuration = new Configuration();configuration.setDefaultEncoding("utf-8");

2.装载数据
数据可来自请求、classpath、数据库等。
Map daaMapt=new HashMap();daaMapt.put...

3.加载模板
//这里我们的模板是放在com.cist.bayonet.mainstat.service.impl包下面configuration.setClassForTemplateLoading(this.getClass(), "/com/cist/bayonet/mainstat/service/impl");
Template t=null;try { //sgxxb_info.ftl为要装载的模板   t = configuration.getTemplate("sgxxb_info.ftl");catch (IOException e) {   e.printStackTrace();}

4、导出模板数据到word
File outFile = new File(path+"/name.doc");Writer out = null;try {    FileOutputStream fos = new FileOutputStream(outFile);    OutputStreamWriter oWriter = new OutputStreamWriter(fos,"UTF-8");//这个地方对流的编码不可或缺,//使用main()单独调用时,应该可以,但是如果是web请求导出时导出后word文档就会打不开,并且包XML文件错误。主要是编码格式不正确,无法解//析。    out = new BufferedWriter(oWriter);} catch (FileNotFoundException e1) {    e1.printStackTrace();
}try {    t.process(dataMap, out);} catch (TemplateException e) {    e.printStackTrace();} catch (IOException e) {    e.printStackTrace();}






0 0
原创粉丝点击