java导出并下载word_java 导出word 并下载

/***

* 执行导出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;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值