@RequestMapping(value = "/doc")
public void doc(HttpServletRequest request, HttpServletResponse response, String wordname, String html) {
try {
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort();
response.setHeader("Content-Disposition", "attachment;filename=" + new String((wordname + ".docx").getBytes("GBK"), "ISO-8859-1") + "");
response.setContentType("application/msword");
OutputStream stream = response.getOutputStream();
MsWord.html2word(html, basePath).save(stream);
stream.flush();
stream.close();
} catch (IOException | Docx4JException e) {
e.printStackTrace();
}
}
private WordprocessingMLPackage html2word(String html,String baseUrl) {
Document doc = Jsoup.parse(html);
doc.outputSettings().syntax(Document.OutputSettings.Syntax.xml).escapeMode(Entities.EscapeMode.xhtml);
WordprocessingMLPackage wordMLPackage = null;
try {
wordMLPackage = WordprocessingMLPackage.createPackage(PageSizePaper.valueOf("A4"), true);
XHTMLImporterImpl xhtmlImporter = new XHTMLImporterImpl(wordMLPackage);
wordMLPackage.getMainDocumentPart().getContent().addAll(xhtmlImporter.convert(doc.html(), baseUrl));
} catch (Docx4JException e) {
e.printStackTrace();
}
return wordMLPackage;
}
doc4j通过html生成doc
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 最近写了一个小功能模块,将html文件转化为pdf文件。 1.需求 现在有这么一个需求:假设App里面有一份htm...
- 1、抗拒学英文 在国内几乎所的编程语言都是外国的,所以学技术必定要学会看英文文档,如果不学英文,是绝对无法从菜鸟转...
- UIColor+CJ a easy way to use UIColor by color name, RGB, ...