JAVA生成Word文档





上图:是Word文档中的内容,也就是模板,为了下面步鄹做铺垫,所以在需要输入数据的地方改成了拼音,


将word文档另存为xml文件.




接下来,上面写的拼音就起到作用了.

打开xml文件.搜索 title.

将Title 改为 ${title}

其他地方一样的修改。

改完后,把文件的后缀名直接改成ftl。


然后呢,直接上代码:

[java]  view plain copy
  1. package com;  
  2.   
  3. import java.io.BufferedWriter;  
  4. import java.io.File;  
  5. import java.io.FileNotFoundException;  
  6. import java.io.FileOutputStream;  
  7. import java.io.IOException;  
  8. import java.io.OutputStreamWriter;  
  9. import java.io.Writer;  
  10. import java.util.ArrayList;  
  11. import java.util.HashMap;  
  12. import java.util.List;  
  13. import java.util.Map;  
  14.   
  15. import freemarker.template.Configuration;  
  16. import freemarker.template.Template;  
  17. import freemarker.template.TemplateException;  
  18.   
  19. public class WordTest {  
  20.       
  21.     private Configuration configuration = null;  
  22.       
  23.     public WordTest(){  
  24.         configuration = new Configuration();  
  25.         configuration.setDefaultEncoding("UTF-8");  
  26.     }  
  27.       
  28.     public static void main(String[] args) {  
  29.         WordTest test = new WordTest();  
  30.         test.createWord();  
  31.     }  
  32.       
  33.     public void createWord(){  
  34.         Map<String,Object> dataMap=new HashMap<String,Object>();  
  35.         getData(dataMap);  
  36.         configuration.setClassForTemplateLoading(this.getClass(), "/com");  //FTL文件所存在的位置  
  37.         Template t=null;  
  38.         try {  
  39.             t = configuration.getTemplate("wordModel.ftl"); //文件名  
  40.         } catch (IOException e) {  
  41.             e.printStackTrace();  
  42.         }  
  43.         File outFile = new File("O:/outFilessa"+Math.random()*10000+".doc");  
  44.         Writer out = null;  
  45.         try {  
  46.             out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile)));  
  47.         } catch (FileNotFoundException e1) {  
  48.             e1.printStackTrace();  
  49.         }  
  50.            
  51.         try {  
  52.             t.process(dataMap, out);  
  53.         } catch (TemplateException e) {  
  54.             e.printStackTrace();  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.         }  
  58.     }  
  59.   
  60.     private void getData(Map<String, Object> dataMap) {  
  61.         dataMap.put("title""标题");  
  62.         dataMap.put("year""2012");  
  63.         dataMap.put("month""2");  
  64.         dataMap.put("day""13");  
  65.         dataMap.put("auditor""唐鑫");  
  66.         dataMap.put("phone""13020265912");  
  67.         dataMap.put("weave""占文涛");  
  68. //      dataMap.put("number", 1);  
  69. //      dataMap.put("content", "内容"+2);  
  70.           
  71.         List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();  
  72.         for (int i = 0; i < 10; i++) {  
  73.             Map<String,Object> map = new HashMap<String,Object>();  
  74.             map.put("number", i);  
  75.             map.put("content""内容"+i);  
  76.             list.add(map);  
  77.         }  
  78.           
  79.           
  80.         dataMap.put("list", list);  
  81.     }  
  82. }  



在刚刚的那个ftl文件中直接编辑:
打开FTL文件,搜索   <w:tr
找到第一个,tr的意思不用解释了吧,代表着一行。
这也意味着我找到了Table中的第一行,但是我需要遍历的不是从第一行开始,而是从第二行。
好的,继续搜索,找到第二个。



在它的头上加一个<#list 你的集合名称 as xxxx>
ok,有开头就有结尾,玩过JSP上的EL表达式应该不会感觉很陌生吧。
搜索 </w:tr>
同样找到第二个,加上</#list>结束符。
好的,这样就没问题了。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值