@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, ...