/***
* 执行导出Word 文档
*@paramid 记录编号
* ****/
public String exportSimpleWord(String id, HttpServletRequest request) throwsIOException, TemplateException {
log.info("执行导出Word 文档==>>[id="+id+"]");
HouseAgent houseAgent=houseAgentService.houseAgent(id);this.insertCommunityName2CommunityArea(houseAgent);
List roleIds=new ArrayList();
List communityIds=new ArrayList();
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 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> imglist = null;try{
imglist=getImg(houseAgent.getHouseImgs(), request);
}catch(Exception e) {//TODO Auto-generated catch block
e.printStackTrace();
}//Configuration用于读取ftl文件
Configuration configuration = newConfiguration();
configuration.setDefaultEncoding("utf-8");/*以下是两种指定ftl文件所在目录路径的方式, 注意这两种方式都是*/
//获取当前类所在路径目录
String pathString = request.getSession().getServletContext().getRealPath("/ExportWord");
configuration.setDirectoryForTemplateLoading(newFile(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> finalList=imglist;
ThreadPools.getInstance().execute(newRunnable() {
@Overridepublic voidrun() {for (Mapmap : finalList) {
File file=new File(map.get("deleteImage").toString());
file.delete();
}
}
});returnnameString;
}