1. 程式人生 > >java導出到Word

java導出到Word

config 另存為 art rac pid ret cat mat print

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 }

java導出到Word