記錄一下導出操作
源碼:
/************ * 導出word 並下載 * @param id 房號記錄編號 * ***********************/ @RequestMapping("/exportAgent") @ResponseBody public void execute(String id, HttpServletRequest request,HttpServletResponse resp) throws Exception { log.info("導出word 並下載==>>[id="+id+"]"); String path = request.getSession().getServletContext().getRealPath("/ExportWord"); String filename = exportSimpleWord(id, request);// 生成文件的文件名稱 這個需要動態的獲取 OutputStream out;// 輸出響應正文的輸出流 InputStream in;// 讀取本地文件的輸入流 // 獲得本地輸入流 File file = new File(path + "\\" + filename); in = new FileInputStream(file); // 設置響應正文的MIME類型 resp.setContentType("Content-Disposition;charset=GB2312"); resp.setHeader("Content-Disposition", "attachment;" + " filename=" + new String(filename.getBytes(), "ISO-8859-1")); // 把本地文件發送給客戶端 out = resp.getOutputStream(); int byteRead = 0; byte[] buffer = new byte[512]; while ((byteRead = in.read(buffer)) != -1) { out.write(buffer, 0, byteRead); } in.close(); out.close(); file.delete(); }
操作類:
/*** * 執行導出Word 文檔 * @param id 記錄編號 * ****/ public String exportSimpleWord(String id, HttpServletRequest request) throws IOException, TemplateException { log.info("執行導出Word 文檔==>>[id="+id+"]"); HouseAgent houseAgent = houseAgentService.houseAgent(id); this.insertCommunityName2CommunityArea(houseAgent); List<Integer> roleIds=new ArrayList<Integer> (); List<String> communityIds=new ArrayList<String> (); roleIds.add(8); communityIds.add(houseAgent.getCommunityId()); //房屋租售管理員 CommunityDesResult CommunityDesResult = communityRpcService.getCommunityDesByCommunityId(houseAgent.getCommunityId()); CommunityDes communityDes = CommunityDesResult.getCommunityDes(); if (null != communityDes) { houseAgent.setUserPhone(communityDes.getCommunityServicePhone()); } // 要填充的數據, 注意map的key要和word中${xxx}的xxx一致 Map<String, Object> dataMap = new HashMap<>(); dataMap.put("hosue_title", StringUtils.isNotBlank(houseAgent.getTitle())?houseAgent.getTitle():"");// 標題 dataMap.put("neirong", StringUtils.isNotBlank(houseAgent.getContent())?houseAgent.getContent():"");// 描述 dataMap.put("renovation", StringUtils.isNotBlank(houseAgent.getRenovation())?houseAgent.getRenovation():"");// 裝修 dataMap.put("type", null!=houseAgent.getHouseType()?houseAgent.getHouseType():"");// 類型 dataMap.put("price", houseAgent.getMoney()+houseAgent.getMoneyUnit());// 售價 dataMap.put("house_size", null!=houseAgent.getHouseSize()?houseAgent.getHouseSize().toString()+"平米":"");// 面積 dataMap.put("layout_type", null!=houseAgent.getLayoutType()?houseAgent.getLayoutType():"");// 戶型 dataMap.put("floors", StringUtils.isNotBlank(houseAgent.getFloors())?houseAgent.getFloors():"");// 樓層 dataMap.put("Rights", null!=houseAgent.getYearLimit()?(houseAgent.getYearLimit()+"年"):"");// 產權 dataMap.put("address", StringUtils.isNotBlank(houseAgent.getAddr())?houseAgent.getAddr():"");// 地址 dataMap.put("xiaoqu", StringUtils.isNotBlank(houseAgent.getCommunityName())?houseAgent.getCommunityName():"");// 小區 dataMap.put("area", StringUtils.isNotBlank(houseAgent.getCommunityErea())?houseAgent.getCommunityErea():"");// 區域 dataMap.put("money", (null!=houseAgent.getMoney()?houseAgent.getMoney()+(StringUtils.isNotBlank(houseAgent.getMoneyUnit())?houseAgent.getMoneyUnit():""):""));// 區域 dataMap.put("rentType", StringUtils.isNotBlank(houseAgent.getRentType())?houseAgent.getRentType():""); dataMap.put("year", null!=houseAgent.getYear()?houseAgent.getYear()+"年":""); dataMap.put("telephone",StringUtils.isEmpty(houseAgent.getUserPhone())?"":houseAgent.getUserPhone()); List<Map<String,Object>> imglist = null; try { imglist = getImg(houseAgent.getHouseImgs(), request); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } // Configuration用於讀取ftl文件 Configuration configuration = new Configuration(); configuration.setDefaultEncoding("utf-8"); /* 以下是兩種指定ftl文件所在目錄路徑的方式, 注意這兩種方式都是 */ // 獲取當前類所在路徑目錄 String pathString = request.getSession().getServletContext().getRealPath("/ExportWord"); configuration.setDirectoryForTemplateLoading(new File(pathString)); String nameString = "house" + System.currentTimeMillis() + ".doc"; // 輸出文檔路徑及名稱 File outFile = new File(pathString + "\\" + nameString + ""); // 以utf-8的編碼讀取ftl文件 Template t; String templateFile=""; //出租 if(houseAgent.getType().intValue()==0){ templateFile="rent.ftl"; //出售 }else if(houseAgent.getType().intValue()==1){ templateFile="sell.ftl"; } t = configuration.getTemplate(templateFile, "utf-8"); dataMap.put("images", imglist); // 圖片 Writer out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), "utf-8"), 10240); t.process(dataMap, out); out.close(); //刪除已下載的圖片 final List<Map<String,Object>> finalList=imglist; ThreadPools.getInstance().execute(new Runnable() { @Override public void run() { for (Map<String, Object> map : finalList) { File file=new File(map.get("deleteImage").toString()); file.delete(); } } }); return nameString; }