方法一
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 = "<[^>]+>";
// 定义一些特殊字符的正则表达式 如:
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();
}