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;
}

发布了104 篇原创文章 · 获赞 13 · 访问量 8万+

猜你喜欢

转载自blog.csdn.net/Liutt55/article/details/100930414