java编辑word文档并导出

public static void CreatWordByModel(String tmpFile, Map<String, String> contentMap, String exportFile) throws Exception{

    InputStream in = null;
    in = new FileInputStream(new File(tmpFile));

    HWPFDocument document = null;
    document = new HWPFDocument(in);
    // 读取文本内容
    Range bodyRange = document.getRange();
    System.out.println(bodyRange.toString());
    System.out.println(bodyRange.text());
    // 替换内容
    for (Map.Entry<String, String> entry : contentMap.entrySet()) {
        bodyRange.replaceText("${" + entry.getKey() + "}", entry.getValue());
    }

    //导出到文件
    try {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        document.write((OutputStream)byteArrayOutputStream);
        OutputStream outputStream = new FileOutputStream(exportFile);
        outputStream.write(byteArrayOutputStream.toByteArray());
        outputStream.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

public Map<String, String> packageMapData(ItCompany itCompany, NetSafeCheck safeCheck) {
Map<String, String> contentMap = new HashMap<String, String>();
try {
contentMap.put(“companyName”,itCompany.getName());
contentMap.put(“startTime”,safeCheck.getStartTime());
contentMap.put(“endTime”,safeCheck.getEndTime());
contentMap.put(“number”,StringUtils.isBlank(safeCheck.getNumber())?"":safeCheck.getNumber());
contentMap.put(“attentionOne”,safeCheck.getAttentionOne() == 1?“☑”:“□”);
contentMap.put(“attentionTwo”,safeCheck.getAttentionTwo() == 1?“☑”:“□”);
contentMap.put(“attentionThree”,safeCheck.getAttentionThree() == 1?“☑”:“□”);
contentMap.put(“attentionFour”,safeCheck.getAttentionFour() == 1?“☑”:“□”);
contentMap.put(“attentionFive”,safeCheck.getAttentionFive() == 1?“☑”:“□”);
contentMap.put(“attentionSix”,safeCheck.getAttentionSix() == 1?“☑”:“□”);
contentMap.put(“attentionSeven”,safeCheck.getAttentionSeven() == 1?“☑”:“□”);
contentMap.put(“attentionEight”,safeCheck.getAttentionEight() == 1?“☑”:“□”);
contentMap.put(“attentionNine”,safeCheck.getAttentionNine() == 1?“☑”:“□”);
contentMap.put(“attentionTen”,safeCheck.getAttentionTen() == 1?“☑”:“□”);
contentMap.put(“contactMan”,StringUtils.isBlank(safeCheck.getContactMan())?"":safeCheck.getContactMan());
contentMap.put(“contactMobile”,StringUtils.isBlank(safeCheck.getContactMobile())?"":safeCheck.getContactMobile());
contentMap.put(“signTime”,“年 月 日”);
contentMap.put(“startTime”,“年 月 日”);
contentMap.put(“endTime”,“年 月 日”);
SimpleDateFormat s2d = new SimpleDateFormat(“yyyy-MM-dd”);
SimpleDateFormat d2s = new SimpleDateFormat(“yyyy 年MM 月dd 日”);
if(StringUtils.isNotBlank(safeCheck.getSignTime())){
Date simpleFormat = s2d.parse(safeCheck.getSignTime());
contentMap.put(“signTime”,d2s.format(simpleFormat));
}
if(StringUtils.isNotBlank(safeCheck.getStartTime())){
Date startTime = s2d.parse(safeCheck.getStartTime());
contentMap.put(“startTime”,d2s.format(startTime));
}
if(StringUtils.isNotBlank(safeCheck.getEndTime())){
Date endTime = s2d.parse(safeCheck.getEndTime());
contentMap.put(“endTime”,d2s.format(endTime));
}
}catch (Exception e){
e.printStackTrace();
}
return contentMap;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值