java poi替换word_利用POI 技术动态替换word模板内容

本文介绍了一个使用Java POI库动态替换Word模板内容的项目需求,包括处理页眉图片、文档水印、下载乱码以及阿拉伯数字转中文繁体显示的问题。提供了具体的代码实现,展示如何读取模板文件、替换字段并输出供下载的Word文档。
摘要由CSDN通过智能技术生成

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

中间解决了问题有:

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

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

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

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

787ce3cc1d1e4722fee7a29e146527db.png

具体代码如下:

/**

* 拍卖结算之后,进行成交确认书的下载操作方法

*

* @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 map = new HashMap();

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 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();

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值