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

时间: 2024-08-02 14:15:22

java导出到Word的相关文章

java导出生成word

最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用.DLL动态链接库的生成需要windows平台的支持.该方案只能在windows平台实现,是其局限性. 2:Apache POI包括一系列的API,它

Java 实现HTML富文本导出至word完美解决方案

一. 问题的提出 最近用java开发一个科技项目信息管理系统,里面有一个根据项目申请书的模板填写项目申报信息的功能,有一个科技项目申请书word导出功能. 已有的实现方式:采用标准的jsp模板输出实现,简单地说,就是把数据渲染进jsp页面,然后将此页面另存为doc文档,从而达到word导出效果.但是存在以下几个问题: (1) 由于导出的html网页格式,打开word后,默认显示的视图模式为WEB版式视图: (2) 修改word文档后,会新增一个相关联的文件夹,word的html中会引用这个文件夹

java将html导出成word ——增加换行 换页符号

java将html导出word增加换行符 //换页 <span lang=EN-US style='font-size:12.0pt;line-height:150%;font-family:"Times New Roman"; mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;mso-ansi-language:EN-US; mso-fareast-language:ZH-CN;mso-bidi-language:AR-SA'&

java将html导出成word(利用的poi包导出)

//导出到word public void exportWord(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest request, HttpServletResponse response) throws Exception { DBTool tool = null ; try { /*** 查询数据库获得数据 **/ ArrayList records = form.getRecords(); if(

java导出word的6种方式(复制来的文章)

来自: http://www.cnblogs.com/lcngu/p/5247179.html 最近做的项目,需要将一些信息导出到word中.在网上找了好多解决方案,现在将这几天的总结分享一下. 目前来看,java导出word大致有6种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.使用Jacob自带的DLL动态链接库,并通过JNI的方式实现了在Java平台上对COM程序的调用.DLL动态链接库的生成需要windows平台的支持.

[原创]java导出word的5种方式

在网上找了好多天将数据库中信息导出到word中的解决方案,现在将这几天的总结分享一下.总的来说,java导出word大致有5种解决方案: 1:Jacob是Java-COM Bridge的缩写,它在Java与微软的COM组件之间构建一座桥梁.通过Jacob实现了在Java平台上对微软Office的COM接口进行调用. 优点:调用微软Office的COM接口,生成的word文件格式规范. 缺点:服务器只能是windows平台,不支持unix和linux,且服务器上必须安装微软Office. 2:Ap

Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享

Java解析OFFICE(word,excel,powerpoint)以及PDF的实现方案及开发中的点滴分享 在此,先分享下写此文前的经历与感受,我所有的感觉浓缩到一个字,那就是:"坑",如果是两个字那就是"巨坑"=>因为这个需求一开始并不是这样子的,且听我漫漫道来: 一开始客户与我们商量的是将office和PDF上传,将此类文件解析成html格式,在APP端调用内置server直接以html"播放" 经历一个月~,两个月~,三个月~~~

Java学习之道:Java 导出EXCEL

1.Apache POI简介 Apache POI是Apache软件基金会的开放源码函式库,POI提供API给Java程式对Microsoft Office格式档案读和写的功能. .NET的开发人员则可以利用NPOI (POI for .NET) 来存取 POI 的功能. 2.POI结构 HSSF - 提供读写Microsoft Excel XLS格式档案的功能. XSSF - 提供读写Microsoft Excel OOXML XLSX格式档案的功能. HWPF - 提供读写Microsoft

java 操作 Excel,java导出excel

WritableWorkbook out = null; try { response.getServletResponse().reset(); ((HttpServletResponse) response.getServletResponse()).setHeader("Content-Disposition", "attachment;filename=export.xls"); response.getServletResponse().setConten