HTML转word
背景介绍
业务:将平台中笔记(富文本)以word形式导出。
1. 使用POI进行转化
依赖jar
poi-3.17.jar
poi-excelant-3.17.jar
poi-ooxml-3.17.jar
poi-ooxml-schemas-3.17.jar
jsoup-1.11.3.jar
1.1 思路
a). 获取HTML
b). HTML标准化
c). 转化
1.2 代码示例
/**
* HTML转word
*
* @param noteName 导出文件名称
* @paramre portDirName 文件路径
* @param researchNoteInfo 文件的html
* @return void
* @author Solitary
* @date 2019/1/11 9:21
*/
public static void htmlToWord(String noteName, String reportDirName, String researchNoteInfo) throws Exception {
//拼一个标准的HTML格式文档
Document document = Jsoup.parse(researchNoteInfo);
FileUtils.fileIsExist(reportDirName);
InputStream is = new ByteArrayInputStream(document.html().getBytes("GBK"));
OutputStream os = new FileOutputStream(reportDirName+noteName);
inputStreamToWord(is, os);
}
/**
* 把is写入到对应的word输出流os中
*
* @param is
* @param os
* @throws IOException
*/
private static void inputStreamToWord(InputStream is, OutputStream os) throws IOException {
POIFSFileSystem fs = new POIFSFileSystem();
DirectoryNode root = fs.getRoot();
root.createDocument("WordDocument", is);
fs.writeFilesystem(os);
os.close();
is.close();
}