一、需求:
我们在使用富文本编辑器来编辑文本的时候,文本会自带HTML的标签比如
等来修饰字体样式。
比如ueditor、kindeditor等富文本编辑器。
那么,我们如何将富文本编辑器里的内容导出到word文档里?
二、思路:
word文档是可以识别完整的html页面的,所以我们需要将首尾缺失的标签补齐。
1.首先我们需要建立一个word导出的工具类:
/**
* html 导出 word 工具类
* @author zhangxiang
*
*/
public class WordUtil {
public static void exportWord(HttpServletRequest request, HttpServletResponse response, String content, String fileName) throws Exception {
byte b[] = content.getBytes("GBK"); //这里是必须要设置编码的,不然导出中文就会乱码。
ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
POIFSFileSystem poifs = new POIFSFileSystem();
DirectoryEntry directory = poifs.getRoot();
DocumentEntry documentEntry = directory.createDocument("WordDocument&