读书人

怎么能让Java生成复杂Word文档(转-夜半

发布时间: 2012-12-24 10:43:14 作者: rapoo

如何能让Java生成复杂Word文档(转-夜半难眠 IT168)
?

?

?

?

主要程序代码:

  1. package?com.havenliu.document; ?import?java.io.BufferedWriter; ?
  2. import?java.io.File; ? import?java.io.FileNotFoundException; ?
  3. import?java.io.FileOutputStream; ? import?java.io.IOException; ?
  4. import?java.io.OutputStreamWriter; ? import?java.io.Writer; ?
  5. import?java.util.ArrayList; ? import?java.util.HashMap; ?
  6. import?java.util.List; ? import?java.util.Map; ?
  7. import?freemarker.template.Configuration; ? import?freemarker.template.Template; ?
  8. import?freemarker.template.TemplateException; ? public?class?DocumentHandler?{ ?
  9. ?? private?Configuration?configuration?=?null; ? ?? public?DocumentHandler()?{ ?
  10. ????? configuration?=?new?Configuration(); ? ????? configuration.setDefaultEncoding("utf-8"); ?
  11. ?? } ??? public?void?createDoc()?{ ?
  12. ????? //要填入模本的数据文件 ? ????? Map?dataMap=new?HashMap(); ?
  13. ????? getData(dataMap); ?????? //设置模本装置方法和路径,FreeMarker支持多种模板装载方法。可以重servlet,classpath,数据库装载, ?
  14. ????? //这里我们的模板是放在com.havenliu.document.template包下面 ? ????? configuration.setClassForTemplateLoading(this.getClass(),?"/com/havenliu/document/template"); ?
  15. ????? Template?t=null; ? ????? try?{ ?
  16. ???????? //test.ftl为要装载的模板 ? ???????? t?=?configuration.getTemplate("test.ftl"); ?
  17. ????? }?catch?(IOException?e)?{ ? ???????? e.printStackTrace(); ?
  18. ????? } ?????? //输出文档路径及名称 ?
  19. ????? File?outFile?=?new?File("D:/temp/outFile.doc"); ? ????? Writer?out?=?null; ?
  20. ????? try?{ ? ???????? out?=?new?BufferedWriter(new?OutputStreamWriter(new?FileOutputStream(outFile))); ?
  21. ????? }?catch?(FileNotFoundException?e1)?{ ? ???????? e1.printStackTrace(); ?
  22. ????? } ?????? try?{ ?
  23. ???????? t.process(dataMap,?out); ?????? }?catch?(TemplateException?e)?{ ?
  24. ???????? e.printStackTrace(); ?????? }?catch?(IOException?e)?{ ?
  25. ???????? e.printStackTrace(); ?????? } ?
  26. ?? } ??? /** ?
  27. ?? *?注意dataMap里存放的数据Key值要与模板中的参数相对应 ??? *?@param?dataMap ?
  28. ?? */??? private?void?getData(Map?dataMap) ?
  29. ?? { ?????? dataMap.put("author",?"张三"); ?
  30. ????? dataMap.put("remark",?"这是测试备注信息"); ? ????? List ?
  31. ????? _table1=new?ArrayList(); ?
  32. ????? Table1?t1=new?Table1(); ? ????? t1.setDate("2010-10-1"); ?
  33. ????? t1.setText("制定10月开发计划内容。"); ? ????? _table1.add(t1); ?
  34. ????? Table1?t2=new?Table1(); ? ????? t2.setDate("2010-10-2"); ?
  35. ????? t2.setText("开会讨论开发计划"); ? ????? _table1.add(t2); ?
  36. ????? dataMap.put("table1",?_table1); ? ????? List ?
  37. ????? _table2=new?ArrayList(); ?
  38. ????? for(int?i=0;i<5;i++) ? ????? { ?
  39. ???????? Table2?_t2=new?Table2(); ? ???????? _t2.setDetail("测试开发计划"+i); ?
  40. ???????? _t2.setPerson("张三——"+i); ? ???????? _t2.setBegindate("2010-10-1"); ?
  41. ???????? _t2.setFinishdate("2010-10-31"); ? ???????? _t2.setRemark("备注信息"); ?
  42. ???????? _table2.add(_t2); ?????? } ?
  43. ????? dataMap.put("table2",?_table2); ? ?? } ?
  44. }?

读书人网 >编程

热点推荐