JAVA FreeMarker导出word

编辑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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值