1、首先設計一個靜態的HTML頁面,根據須要導出word設計佈局。html
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>互動建議列表</title> </head> <body> <h3 style="text-align: center;">互動標題</h3> <div> <div>互動內容</div> <p style="text-align: right;"><span>建立人:張三 </span><span>建立時間:2016-02-03</span></p> </div> <h5 style="text-align: center;">意見列表</h5> <table style="width: 100%;border: none;" cellspacing="0" cellpadding="0"> <tbody > <tr > <td style="width: 25%;text-align: right;border-top:none;border-left:none;border-bottom:none;border-right:none;">建立人:</td> <td style="width: 25%;border-top:none;border-left:none;border-bottom:none;border-right:none;">張三</td> <td style="width: 25%;text-align: right;border-top:none;border-left:none;border-bottom:none;border-right:none;">建立時間:</td> <td style="width: 25%;border-top:none;border-left:none;border-bottom:none;border-right:none;">2016-02-03</td> </tr> <tr> <td style="text-align: right;border-top:none;border-left:none;border-bottom:none;border-right:none;">意見內容:</td> <td colspan="3" style="border-top:none;border-left:none;border-bottom:none;border-right:none;" >1111</td> </tr> </tbody> </table> </body> </html>
注:表格只要加邊框就好,轉換後自動會有內邊框java
2、把設計好的頁面,更名爲ftl後綴node
3、後臺代碼app
(1)把poi相關依賴包,放入項目中
佈局
(2)具體實現this
/** * * <p>【導出doc文檔】</p> * <p>條件:</p> * <p>備註:</p> * <p>例子:</p> * <p>日誌:</p> * * @author:zhu [2016年2月3日 下午5:11:04] */ public void outOfDocGeneral() { StringWriter stringOut = new StringWriter(); ServletOutputStream out = null; ByteArrayInputStream bais = null; try { //獲取數據 hdJyList = hdJyService.findByCreater(hd.getId(), getCurrentUser().getId()); hd = hdService.load(hd.getId()); Configuration configuration = new Configuration(); configuration.setDefaultEncoding("UTF-8"); //獲取模板根路徑 configuration.setClassForTemplateLoading(this.getClass(), "../../../../../docTemplate"); //設置對於填充數據 Map<String, Object> dataMap = new HashMap<String, Object>(); dataMap.put("title", hd.getHdbt()); dataMap.put("creater", hd.getLrr()); org.jsoup.nodes.Document doc = Jsoup.parse(hd.getHdnr()); Elements eles = doc.getElementsByTag("img"); String url = getUrl(); for (Element element : eles) { element.attr("src", url + element.attr("src")); } dataMap.put("content", doc.html()); dataMap.put("createTime", hd.getLrsj()); dataMap.put("hdJyList", hdJyList); //獲取模板 Template temp = configuration.getTemplate("hdJyList.ftl"); //填充模板 temp.process(dataMap, stringOut); //獲取整個HTML字符串 String content = stringOut.toString(); byte b[] = content.getBytes("UTF-8"); bais = new ByteArrayInputStream(b); POIFSFileSystem poifs = new POIFSFileSystem(); DirectoryEntry directory = poifs.getRoot(); DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); out = super.getWWResponse().getOutputStream();// 取得輸出流 super.getWWResponse().reset();// 清空輸出流 super.getWWResponse().setHeader("Content-disposition", "attachment; filename=proposal.doc");// 設定輸出文件頭 super.getWWResponse().setContentType("application/vnd.ms-word;charset=UTF-8");// 定義輸出類型 poifs.writeFilesystem(out); bais.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { if (bais != null) { try { bais.close(); } catch (IOException e) { e.printStackTrace(); } } if (stringOut != null) { try { stringOut.close(); } catch (IOException e) { e.printStackTrace(); } } if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * * <p>【獲取當前請求url】</p> * <p>條件:</p> * <p>備註:</p> * <p>例子:</p> * <p>日誌:</p> * * @return * @author:zhu [2016年2月3日 下午5:00:38] */ private String getUrl() { String url = ""; url = super.getWWRequest().getScheme() + "://" + super.getWWRequest().getServerName() + ":" + super.getWWRequest().getServerPort() + super.getWWRequest().getContextPath() + "/"; return url; }
4、頁面url
//後綴有個 .doc,頁面就不會打開,自動下載關閉頁面 window.open (url+"/hdAction!outOfDocGeneral.shtml?file=proposal.doc");