业务:

将富文本内容取出生成本地word文件

参考百度的方法

word本身是可以识别html标签,所以通过poi写入html内容即可

1、引用poi相关jar

2、直接生成本地doc文件(已测试)

public static Attachment createWordForHtml(String html,String fileName,String type) {Attachment uploadAttachment = null;try {String dateDir = DateUtils.formatDate(System.currentTimeMillis(), "yyyyMMdd");String savePath = dateDir+File.separator;File file = new File(Global.getConfig("userfiles.basedir")+File.separator+savePath);if(!file.exists()) {file.mkdirs();}savePath = Global.getConfig("userfiles.basedir")+File.separator+savePath+fileName+".doc"; //文件路径地址    :D:\apps\legislation\20210126\xxx.doc   //取出来的数据为转义的     先转一下html = html.replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&amp;", "&");//word识别的html  必须是完整的     加上头和尾String content="<html><body>"+html+"</body></html>";   byte b[] = content.getBytes("GBK");  //这里是必须要设置编码的,不然导出中文就会乱码。ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中  /** 关键地方* 生成word格式 */POIFSFileSystem poifs = new POIFSFileSystem();  DirectoryEntry directory = poifs.getRoot();  DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); //这块需要加上   直接 poifs.createDocument(stream, name) 不行OutputStream ostream = new FileOutputStream(savePath); //导出到本地代码poifs.writeFilesystem(ostream);  bais.close();  ostream.close(); //此处为持久化   不用看
//              File dirFile = new File(savePath);
//              FileInputStream fileInputStream = new FileInputStream(dirFile);
//              MultipartFile multipartFile = new MockMultipartFile(dirFile.getName(), dirFile.getName(),
//                  ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
//              uploadAttachment = FileUtils.uploadMultipartFile(multipartFile);
//              uploadAttachment.setBusinessType(type);
//              attachmentService.save(uploadAttachment);
//              fileInputStream.close();} catch (Exception e) {e.printStackTrace();}return uploadAttachment;}

简化:

public static void createWordForHtml(String html,String fileName) {try {String savePath = "文件路径"+fileName+".doc"; html = html.replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&amp;", "&");String content="<html><body>"+html+"</body></html>";   byte b[] = content.getBytes("GBK");  //这里是必须要设置编码的,不然导出中文就会乱码。ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中  /** 关键地方* 生成word格式 */POIFSFileSystem poifs = new POIFSFileSystem();  DirectoryEntry directory = poifs.getRoot();  DocumentEntry documentEntry = directory.createDocument("WordDocument", bais); OutputStream ostream = new FileOutputStream(savePath);poifs.writeFilesystem(ostream);    //写入内容bais.close();  ostream.close(); } catch (Exception e) {e.printStackTrace();}}

3。前端直接下载(未测试)


//未测试   编码格式可能需要修改
public void exportWord( HttpServletRequest request, HttpServletResponse response) throws Exception {     try {    //word内容String content="<html><body></body></html>";   byte b[] = content.getBytes("utf-8");  //这里是必须要设置编码的,不然导出中文就会乱码。ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中  /** 关键地方* 生成word格式 */POIFSFileSystem poifs = new POIFSFileSystem();  DirectoryEntry directory = poifs.getRoot();  DocumentEntry documentEntry = directory.createDocument("文档名称", bais); //输出文件request.setCharacterEncoding("utf-8");  response.setContentType("application/msword");//导出word格式response.addHeader("Content-Disposition", "p_w_upload;filename=" +                            new String( (documentEntry.getName() + ".doc").getBytes(),  "iso-8859-1"));OutputStream ostream = response.getOutputStream(); poifs.writeFilesystem(ostream);  bais.close();  ostream.close(); }catch(Exception e){//异常处理}  }

java 将html转为word导出 (富文本内容导出word)相关推荐

  1. Java 富文本内容转化word导出

    一.需求: 当创建使用富文本编辑器,操作完的数据,传输到后台都是带有html标签的. 如:<h1>标题头</h1><h2>第二个标题</h2><a ...

  2. 【操作word】Java + POI导出富文本的内容到word文档

    这周工作中,遇到一个需求是需要将数据库中富文本内容导出到word文档里面,于是就采用POI技术实现了一下导出word文档的功能.(word文档是识别html内容的,所以富文本内容也自然能够识别.) 一 ...

  3. 导出富文本格式word

    /*try {*//*** 关键地方* 生成word格式*//*POIFSFileSystem poifs = new POIFSFileSystem();DirectoryEntry directo ...

  4. java freemarker 图片_java通过freemarker导出包含富文本图片的word文档

    废话不多说,进入正题! 本文重点在于:对富文本图片的导出(基础的freemarker+word模板导出这里不做详细解说哈) (ps:大神的东西太深奥~~懵逼了 一周才搞定,为了方便后来在更加简单,清晰 ...

  5. Java web/springboot上传word/doc/docx文档(含图片)与HTML富文本导入/导出互相转换解析!附项目源码

    测试效果 先看下效果 文档内容如下: 上传 上传docx文档 查看解析内容 <html><head><style>p{margin-top:0pt;margin-b ...

  6. java 富文本 word_Java导出富文本到word

    源码地址: 背景 最近用java开发一个中车项目管理系统,里面有一个维修单word导出功能. 可用方案 在网上查找资料,总结出两种比较可行的方案. (1) 制作word模板,导出成mht文件(单页面网 ...

  7. java freemarker 导出富文本到Word文档

    最近一直在加班 导致看到代码就想吐 今天抽出时间来记录一下 用freemaker导出富文本信息 之前使用freemaker导出图片等信息时 使用的是xml方式,不明白的 可以看 freemaker生成 ...

  8. java实现word导入导出富文本(含图片)-附完整测试用例

    主要有以下几点: 1.解决富文本导入导出依赖兼容问题 2.处理富文本和非富文本内容 3.解决webp格式通过java下载不了问题,如果要用到富文本导出,将来势必是会碰到的bug,这里提前给提出来并解决 ...

  9. word读入富文本编辑器,编辑后导出下载word日常总结

    基本思路就是: 使用poi读入word文档, 读入内容转为html内容,(直接读入text仅仅只是文本没有格式) 将html内容给富文本编辑器显示(这样的目的是带格式). 下边贴代码: poi读入wo ...

  10. springboot导出富文本框数据到word

    这里需要注意的是:依赖的版本号都是一一对应的,如果使用不兼容的版本号,可能会报编译异常和缺少类的错误,这里已提供对应的所需依赖和版本号,大家可以参考一下. 具体的api文档可以参考官网api,很好理解 ...

最新文章

  1. 如何在Eclipse中查看JDK以及JAVA框架的源码(转载)
  2. Bean 在 Spring 中代表什么含义,为什么这样命名?
  3. 身体容易缺少的微量元素
  4. 组件Refs(操作DOM的2⃣️两种方法)
  5. [置顶] 程序员编程生产力相差10倍意味着什么?
  6. Ant Design Form.Item的label中文字换行的替代方式
  7. mysql 5.7多层级json查询_MySql5.7 json查询
  8. Oracle11g安装教程
  9. Zotero数据及文件同步
  10. IntelliJ IDEA安装与JDK 环境变量配置
  11. 解决vscode下载太慢的问题
  12. 2020语义分割网络语义流:Semantic Flow for Fast and Accurate Scene Parsing
  13. activiti 获取审批人员_activiti,根据任务id获取该任务节点配置的获选人或者获选组...
  14. linux img 转 iso,Ubuntu下将img 转化成iso
  15. 解读微信多开技巧,Python tk 实现微信多开脚本exe工具
  16. 电子元件中场效应晶体管与晶体三极管,谁能领袖群伦
  17. 【洛谷 P5550】 Chino的数列【矩阵乘法】
  18. _搭积木 java
  19. JPA 菜鸟教程 18 自动把firstName+lastName合并为name字段
  20. 日本自动外币兑换机公司ActPro在两年半时间内占据全球市场份额第一名

热门文章

  1. 用c语言求积分程序,菜鸟学C语言(五)之求定积分
  2. 5G工业路由器5G工业网关的区别
  3. FLASH抽象层(FAL)程序的应用(rt-thread)
  4. python 红黑树_手把手教你43行代码写红黑树(包括删除操作)
  5. Flask学习笔记[更新中....]
  6. 菜鸟的Vue基础快速入门
  7. matplotlib画正态分布图
  8. VMware虚拟机提速10招
  9. vue 时间轴:效果图
  10. excel表格打印每页都有表头_excel单页表格打印带连续页码的多页