富文本编辑的内容导出为word
发布日期:2021-06-28 18:30:47 浏览次数:3 分类:技术文章

本文共 2931 字,大约阅读时间需要 9 分钟。

1、集成富文本编辑器,博主,采用ckeditor

引入相关的jar包

cn.afterturn
easypoi-base
3.2.0
cn.afterturn
easypoi-annotation
3.2.0

2、封装工具类

package top.cfl.cflwork.util.excel;import cn.afterturn.easypoi.word.WordExportUtil;import cn.hutool.core.lang.Assert;import org.apache.poi.poifs.filesystem.DirectoryEntry;import org.apache.poi.poifs.filesystem.DocumentEntry;import org.apache.poi.poifs.filesystem.POIFSFileSystem;import org.apache.poi.xwpf.usermodel.XWPFDocument;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.ByteArrayInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.OutputStream;import java.net.URLEncoder;import java.util.Map;public class ExcelUtil {    /**     * 导出word方法     * @param request     * @param response     * @param html     * @param title     */    public static void exportWords(HttpServletRequest request, HttpServletResponse response, String html, String title) {        ServletOutputStream ostream = null;        POIFSFileSystem poifs = null;        ByteArrayInputStream bais = null;        try {            String content = html;            //设置编码            byte b[] = content.getBytes("utf-8");            bais = new ByteArrayInputStream(b);            poifs = new POIFSFileSystem();            DirectoryEntry directory = poifs.getRoot();            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);            request.setCharacterEncoding("utf-8");            response.setContentType("application/msword");            response.addHeader("Content-Disposition", "attachment;filename=" +                    new String(title.getBytes("GB2312"), "iso8859-1") + ".doc");            ostream = response.getOutputStream();            poifs.writeFilesystem(ostream);        } catch (Exception e) {            e.printStackTrace();        } finally {            try {                if (bais != null) {                    bais.close();                }                if (ostream != null) {                    ostream.close();                }                if (poifs != null) {                    poifs.close();                }            } catch (Exception ex) {                ex.printStackTrace();            }        }    }}

3、调用工具类

@GetMapping("/exportItem/{id}")    public void export(HttpServletRequest request, HttpServletResponse response, @PathVariable("id") String id) {        SysNotice sysNotice = sysNoticeService.findSysNoticeById(id);        exportWords(request,response,sysNotice.getContent(),sysNotice.getTitle());//        exportWord("word/message.docx",  "通知公告.docx", convertBeanToMap(sysNotice), request, response);    }

4、导出结果

5、总结,导出的内容,跟编辑器一样,只是导出后,打开是web板式。需要切换至写作板式。

转载地址:https://blog.csdn.net/xljx_1/article/details/102753617 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:ElementUI 树形结构默认展示某个节点
下一篇:SpringBoot 集成webSocket 打成jar包ba

发表评论

最新留言

逛到本站,mark一下
[***.202.152.39]2024年04月07日 13时29分35秒