java导出word(2003版doc格式) FreeMarker+XML实现

来源:互联网 发布:数据库营销 培训 编辑:程序博客网 时间:2024/05/21 08:56

FreeMarker+XML实现写word

最近在接触对于word的读写操作,以前用的是jacob,但是有弊端(配置麻烦+平台限制),无意间看到了用Freemarker+XML的方式也可以操作word,而且样式根本不会丢,也不受平台限制。花了一天时间琢磨出来点东西,今天给大家呈上。

解释下原理:word从2003版就支持xml格式,而freemarker是java封装的模板工具,两者结合也就是在xml中需要动态生成的部分调用freemarker的指令(类似于EL表达式),来生成我们需要的数据,再用流输出文件,就达到了写word的效果。

优点: 跨平台、样式不会丢失
缺点: 模板制作比较麻烦

ps:这里只是简单的演示,具体的需要根据项目需要来完成需求。

下面我为大家展示一下具体实现方法:

  1.需要自定义一个模板,可以自己新建一个word文件,样式和假数据填充完成后,另存为xml格式,修改成模板,如下图:



2.编写用freemarker来处理xml的工具类  xmltoword.java 代码如下:

public class XmlToExcel {private static XmlToExcel tplm = null; private Configuration cfg = null; private XmlToExcel() {  cfg = new Configuration(); try {  //注册tmlplate的load路径   cfg.setClassForTemplateLoading(this.getClass(), "/template/");  } catch (Exception e) {     } } private static Template getTemplate(String name) throws IOException {  if(tplm == null) {   tplm = new XmlToExcel();  }  return tplm.cfg.getTemplate(name); }  /**  *   * @param templatefile 模板文件  * @param param        需要填充的内容  * @param out填充完成输出的文件  * @throws IOException  * @throws TemplateException  */ public static void process(String templatefile, Map param ,Writer out) throws IOException, TemplateException{  //获取模板  Template template=XmlToExcel.getTemplate(templatefile);  template.setOutputEncoding("UTF-8");  //合并数据  template.process(param, out);  if(out!=null){out.close();} }public static void main(String[] args) {//测试数据 Map<String,Object> param = new HashMap<String, Object>();  param.put("test","我的测试数据");  try {Writer out = new OutputStreamWriter(new FileOutputStream(new File("d:\\test.doc")),"UTF-8");XmlToExcel.process("test.xml", param,out);} catch (IOException e) {e.printStackTrace();} catch (TemplateException e) {e.printStackTrace();}}}

其实就是调用template的process方法来合并数据,然后freemarker通过writer来输出文件。

模板所在的路径:template/test.xml


3.运行完成后出现的结果,如图:


这里标题变成了在工具类中定义的map中的数据。

这里只是生成doc格式的数据 ,下一篇将会介绍如何生成docx格式的word




1 0