记录一下导出操作
源码:
/************ * 导出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; }