博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
利用POI 技术动态替换word模板内容
阅读量:4532 次
发布时间:2019-06-08

本文共 2832 字,大约阅读时间需要 9 分钟。

项目中需要实现一个功能,动态替换给定模板里面的内容,生成word文档提供下载功能。

中间解决了问题有:

1.页眉的文档logo图片解决,刚开始的时候,HWPFDocument 对象无法读取图片对象(已测试)

2.文档的水印也无法读取

3.下载的乱码问题(火狐浏览器)

4.将文档中的阿拉伯数字的金额改为中文繁体显示

 

 

 

具体代码如下:

/**

* 拍卖结算之后,进行成交确认书的下载操作方法
*
* @param id
* @param response
*/
@RequestMapping(value="/aucLotDownLoad",method = RequestMethod.GET)
@ResponseBody
public void aucLotDownLoad(String id,HttpServletResponse response) {
if (logger.isDebugEnabled()) {
logger.debug("aucLotQuery, aucLotId ", id);
}
SimpleDateFormat dateFormater = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
AucLot aucLot = aucLotRepository.findOne(id);
AucBrand aucBrand = aucBrandRepository.findOneByAucLotAndIsBailPayedAndIsDealAndIsSettled(aucLot,AucBrand.AUCBRAND_ISBAILPAYED_YES,AucBrand.AUCBRAND_ISDEAL_SUCCESS,AucBrand.AUCBRAND_ISSETTLED_YES);
if (aucBrand != null) {
String goodsName = aucLot.goodsName();//标的名称
String brandNo = aucBrand.brandNo();//买受人号牌,竞买代码
Date startTime = aucLot.startTime();//拍卖开始时间
Date endTime = aucLot.endTime();//拍卖结束时间
BigDecimal dealPrice = aucBrand.dealPrice();//拍卖成交金额
BigDecimal clientCommison = aucLot.clientCommison();//委托佣金
//定义成交价和委托佣金的总和(两种方式体现)
BigDecimal totalPrice = dealPrice.add(clientCommison);//合计拍卖的总金额
try {
//获取模板文件的目录地址
String fileDir = new File(base.getFile(), "../../../../../../doc/").getCanonicalPath();
//获取模板文件
File demoFile=new File(fileDir + "/1.doc");
FileInputStream in = new FileInputStream(demoFile);
HWPFDocument hdt = new HWPFDocument(in);
//替换读取到的word模板内容的指定字段
Range range = hdt.getRange();
Map<String, String> map = new HashMap<String, String>();
map.put("$PMBD$", goodsName);
map.put("$PMKSSJ$", dateFormater.format(startTime));
map.put("$MSRHP$", brandNo);
map.put("$PMCJJ$", numberToHanZiUtility.number2CNMontrayUnit(dealPrice));
map.put("$PMYJ$", numberToHanZiUtility.number2CNMontrayUnit(clientCommison));
map.put("$HJ$", numberToHanZiUtility.number2CNMontrayUnit(totalPrice));
map.put("$PMCJJXX$", dealPrice + "");
map.put("$PMYJXX$", clientCommison + "");
map.put("$HJXX$", totalPrice + "");
map.put("$PMJSSJ$", dateFormater.format(endTime));
for (Map.Entry<String,String> entry:map.entrySet()) {
range.replaceText(entry.getKey(),entry.getValue());
}
//输出word内容文件流,提供下载
response.setContentType("application/x-msdownload");
String name = java.net.URLEncoder.encode("成交确认书_"+aucLot.goodsNo()+".doc", "UTF8");
name = new String((name).getBytes("UTF-8"), "ISO-8859-1");
response.addHeader("Content-Disposition", "attachment; filename*=utf-8'zh_cn'"+name);
ByteArrayOutputStream ostream = new ByteArrayOutputStream();
ServletOutputStream servletOS = response.getOutputStream();
hdt.write(ostream);
servletOS.write(ostream.toByteArray());
servletOS.flush();
servletOS.close();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}

 

 

 

}

 

转载于:https://www.cnblogs.com/mr-wuxiansheng/p/6131494.html

你可能感兴趣的文章
XML文件生成C++代码(基于rapidxml)
查看>>
写代码,更需要设计代码
查看>>
iOS:修改项目名
查看>>
SpringCloud-Eureka
查看>>
double在输出为字符串的几种方法效率测试
查看>>
ArcGIS API for JavaScript 4.2学习笔记[14] 弹窗的位置、为弹窗添加元素
查看>>
电路基础
查看>>
jquery 对象与DOM对象转换
查看>>
DELPHI 调用系统 ADO 配置窗体 提高软件易用性
查看>>
Mongodb 命令及 PyMongo 库的使用
查看>>
div+css 兼容ie6 ie7 ie8 ie9和FireFox Chrome等浏览器方法(非原创)
查看>>
关于SDWebImage加载高清图片导致app崩溃的问题
查看>>
如何查看方法在哪里被调用
查看>>
HUE的自动化安装部署
查看>>
图片服务器(FastDFS)的搭建
查看>>
myBatis应用
查看>>
RuntimeError: DataLoader worker (pid 18255) is killed by signal: Killed.
查看>>
[PHP] 用AppServ一步到位安装PHP服务器
查看>>
mac brew install redis 报错
查看>>
Work? working!
查看>>