html转word

方法一

public void download(String id, HttpServletResponse response) {
    TypicalCaseDTO typicalCaseDTO = get(id);
    if (typicalCaseDTO == null) {
        return;
    }
    String content = typicalCaseDTO.getContent();
    String title = typicalCaseDTO.getTitle();
    String viceTitle = typicalCaseDTO.getViceTitle();
    String caseIdea = typicalCaseDTO.getCaseIdea();
            Map<String, Object> dataMap = new HashMap();
            /// 组装数据
            /// 定义HTML标签的正则表达式
            String regEx_html = "<[^>]+>";
            // 定义一些特殊字符的正则表达式 如:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            String regEx_special = "\\&[a-zA-Z]{1,10};";
            Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            Matcher m_html = p_html.matcher(content);
            content = m_html.replaceAll("");
            Pattern p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
            Matcher m_special = p_special.matcher(content);
            content = m_special.replaceAll(" ");
            dataMap.put("title", title);
            dataMap.put("content", content);
            if (StringUtils.isNotEmpty(viceTitle)) {
                dataMap.put("viceTitle", viceTitle);
            } else {
                dataMap.put("viceTitle", "");
            }
            dataMap.put("caseIdea", caseIdea);
            response.setContentType("application/msword;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(title,"UTF-8")+".doc");
            ExportWordUtils.createWord(dataMap, "case.ftl", response.getOutputStream());
           } catch (Exception e) {
            logger.error("download case file error", e);
        }
    }
}
public static void createWord(Map dataMap, String templateName, OutputStream outputStream) throws Exception {
    ///创建配置实例
    Configuration configuration = new Configuration();
    ///设置编码
    configuration.setDefaultEncoding("UTF-8");
    ///ftl模板文件
    configuration.setClassForTemplateLoading(ExportWordUtils.class, "/");
    ///获取模板
    Template template = configuration.getTemplate(templateName);
    try (Writer out = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"))) {
        ///生成文件
        template.process(dataMap, out);
        ///关闭流
        out.flush();
    }
}

方法二:

 public void download(String id, HttpServletResponse response) {
    TypicalCaseDTO typicalCaseDTO = get(id);
    if (typicalCaseDTO == null) {
        return;
    }
    String content = typicalCaseDTO.getContent();
    String title = typicalCaseDTO.getTitle();
    String viceTitle = typicalCaseDTO.getViceTitle();
    String caseIdea = typicalCaseDTO.getCaseIdea();
    String head1 = "案例要旨";
    String head2 = "案例正文";
    if (StringUtils.isNotEmpty(title) && StringUtils.isNotEmpty(content)) {
        try {
            content = "<html>\n" +
                    "\n" +
                    "<head>\n" +
                    "</head>\n" +
                    "\n" +
                    "<body>\n" +
                    "<h3>\n" + title +
                    "</h3>\n"+
                    "<p>\n" + viceTitle +
                    "</p>\n"+
                    "<h3>"+ head1+
                    "</h3>\n"+
                    "<p>\n" + caseIdea +
                    "</p>\n"+
                    "<h3>"+ head2+
                    "</h3>\n"+
                    "<p style=\"font-size:14px\">\n"+
                    content +
                    "</p>\n"+
                    "</body>\n" +
                    "\n" +
                    "</html>";
            response.setContentType("application/msword;charset=UTF-8");
            response.setHeader("Pragma", "no-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + URLEncoder.encode(title,"UTF-8")+".doc");
            ExportWordUtils.createWord(dataMap, "case.ftl", response.getOutputStream());
            htmlToWord(content,response.getOutputStream());
        } catch (Exception e) {
            logger.error("download case file error", e);
        }
    }
}

public void htmlToWord(String content, OutputStream os) throws Exception {
    InputStream is = new ByteArrayInputStream(content.getBytes("UTF-8"));
    this.inputStreamToWord(is, os);
}

/**
 * 把is写入到对应的word输出流os中
 * 不考虑异常的捕获,直接抛出
 * @param is
 * @param os
 * @throws IOException
 */
private void inputStreamToWord(InputStream is, OutputStream os) throws IOException {
    POIFSFileSystem fs = new POIFSFileSystem();
    /// 对应于org.apache.poi.hdf.extractor.WordDocument
    fs.createDocument(is, "WordDocument");
    fs.writeFilesystem(os);
    os.close();
    is.close();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值