您的位置:首页 > 编程语言 > Java开发

JAVA FreeMarker导出word

2015-05-26 23:08 225 查看

编辑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();
}




内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: