java导出到Word

1、操作步骤:

 (1)、新建word模板,凡是需要填充的数据用${xxxx},编辑好word文档后,另存为word文档的(*.xml),命名为mediteAgreementBook.xml(名字随意更改)

 (2)、在web项目中webroot目录下新建exptemplate文件夹,并将mediteAgreementBook.xml文件放到exptemplate文件夹下

 (3)、拷贝DocUtil.java工具类到项目中,如下面的调用实例
   (4)、返回相对路径,自动下载Word文档

2、添加maven依赖

<dependency>  
    <groupId>org.freemarker</groupId>  
    <artifactId>freemarker</artifactId>  
    <version>2.3.23</version>  
</dependency> 

3、Java代码

 1 @Override
 2     public String createMediateAgreementBookByCaseId(Long id) {
 3         TrafficAccidentJudicial param = new TrafficAccidentJudicial();
 4         param.setCaseId(id);
 5         List<TrafficAccidentJudicial> listData = trafficAccidentJudicialDao.getMediateAgreementBookDataByCaseId(param);
 6         Map<String,String> map = new HashMap<>();
 7         map.put("no", listData.get(0).getAccidentNo() != null ? listData.get(0).getAccidentNo() : "");
 8         
 9         map.put("na1", listData.get(0).getName() != null ? listData.get(0).getName() : "");
10         map.put("s1", listData.get(0).getSexName());
11 //        map.put("n1", listData.get(0).getNationName());
12         map.put("n1", "");
13         map.put("a1", listData.get(0).getAge() != null ? listData.get(0).getAge() + "" : "");
14         map.put("c1", listData.get(0).getCardNo() != null ? listData.get(0).getCardNo() : "");
15         map.put("j1", listData.get(0).getVocation() != null ?  listData.get(0).getVocation() : "");
16         map.put("t1", listData.get(0).getTelephone() != null ? listData.get(0).getTelephone() : "");
17         map.put("ad1", listData.get(0).getPresentAddress() != null ? listData.get(0).getPresentAddress() : "");
18         
19         map.put("na2", listData.get(1).getName() != null ? listData.get(1).getName() : "");
20         map.put("s2", listData.get(1).getSexName());
21 //        map.put("n1", listData.get(0).getNationName());
22         map.put("n2", "");
23         map.put("a2", listData.get(1).getAge() != null ? listData.get(1).getAge() + "" : "");
24         map.put("c2", listData.get(1).getCardNo() != null ? listData.get(1).getCardNo() : "");
25         map.put("j2", listData.get(1).getVocation() != null ?  listData.get(1).getVocation() : "");
26         map.put("t2", listData.get(1).getTelephone() != null ? listData.get(1).getTelephone() : "");
27         map.put("ad2", listData.get(1).getPresentAddress() != null ? listData.get(1).getPresentAddress() : "");
28         
29         map.put("dis", listData.get(1).getDisputeMatter() != null ? listData.get(1).getDisputeMatter() : "");
30         map.put("me", listData.get(1).getMediateRecord() != null ? listData.get(1).getMediateRecord() : "");
31         map.put("ad", listData.get(1).getAgreementDate() != null ? listData.get(1).getAgreementDate() : "");
32         
33          Calendar nowDate = Calendar.getInstance();
34         
35         map.put("y", nowDate.get(Calendar.YEAR) + "");
36         map.put("m",  (nowDate.get(Calendar.MONTH) + 1) + "");
37         map.put("d",  nowDate.get(Calendar.DAY_OF_MONTH) + "");
38         
39         
40         String newWordName = "测试文件名称.doc";
41         HttpServletRequest request =  ServletActionContext.getRequest();  //Struts2获取request
42         HttpServletResponse response = ServletActionContext.getResponse(); //Struts2获取response
43         return exportWord(request, response, newWordName, map);
44     }
45 
46 private String exportWord(HttpServletRequest request,HttpServletResponse response,String newWordName,Map<String, String> dataMap) {
47         @SuppressWarnings("deprecation")
48         Configuration configuration = new Configuration();
49         configuration.setDefaultEncoding("utf-8");   //注意这里要设置编码
50  
51       //模板文件mediteAgreementBook.xml是放在WebRoot/exptemplate目录下的
52         configuration.setServletContextForTemplateLoading(request.getSession()
53                 .getServletContext(), "/exptemplate");
54  
55         Template t = null;
56         try {
57             //word.xml是要生成Word文件的模板文件
58             t = configuration.getTemplate("mediteAgreementBook.xml","utf-8"); // 文件名 还有这里要设置编码
59         } catch (Exception e) {
60             e.printStackTrace();
61         }
62         File outFile = null;
63         Writer out = null;
64         try {
65             outFile = new File(FileUtil.getWebRoot() + GridProperties.EXPORTPATH + File.separator+ newWordName);    //导出文档存放绝对路径
66             out = new BufferedWriter(new OutputStreamWriter(
67                     new FileOutputStream(outFile),"utf-8"));   //还有这里要设置编码
68             t.process(dataMap, out);
69             out.flush();
70             out.close();
71             return  GridProperties.EXPORTPATH + File.separator + File.separator + newWordName;    //返回相对路径
72         } catch (Exception e1) {
73             e1.printStackTrace();
74         }
75         return null;
76     }

 

转载于:https://www.cnblogs.com/doublet/p/9990461.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值