Java实现将word转换为html的方法示例【doc与docx格式】

发布于 2020-6-14|

复制链接

摘记: 本文实例讲述了Java实现将word转换为html的方法。分享给大家供大家参考,具体如下:

```java

public static void main(String[] args) throws Exception {

String filePath = "C:/Users/Administrator/Desktop/92个诊疗方案及临 ..

本文实例讲述了Java实现将word转换为html的方法。分享给大家供大家参考,具体如下:

```java

public static void main(String[] args) throws Exception {

String filePath = "C:/Users/Administrator/Desktop/92个诊疗方案及临床路径/";

File file = new File(filePath);

File[] files = file.listFiles();

String name = null;

for (File file2 : files) {

Thread.sleep(500);

name = file2.getName().substring(0, file2.getName().lastIndexOf("."));

System.out.println(file2.getName());

if (file2.getName().endsWith(".docx") || file2.getName().endsWith(".DOCX")) {

CaseHtm.docx(filePath ,file2.getName(),name +".htm");

}else{

CaseHtm.dox(filePath ,file2.getName(),name +".htm");

}

}

}

/**

* 转换docx

* @param filePath

* @param fileName

* @param htmlName

* @throws Exception

*/

public static void docx(String filePath ,String fileName,String htmlName) throws Exception{

final String file = filePath + fileName;

File f = new File(file);

// ) 加载word文档生成 XWPFDocument对象

InputStream in = new FileInputStream(f);

XWPFDocument document = new XWPFDocument(in);

// ) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)

File imageFolderFile = new File(filePath);

XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));

options.setExtractor(new FileImageExtractor(imageFolderFile));

options.setIgnoreStylesIfUnused(false);

options.setFragment(true);

// ) 将 XWPFDocument转换成XHTML

OutputStream out = new FileOutputStream(new File(filePath + htmlName));

XHTMLConverter.getInstance().convert(document, out, options);

}

/**

* 转换doc

* @param filePath

* @param fileName

* @param htmlName

* @throws Exception

*/

public static void dox(String filePath ,String fileName,String htmlName) throws Exception{

final String file = filePath + fileName;

InputStream input = new FileInputStream(new File(file));

HWPFDocument wordDocument = new HWPFDocument(input);

WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());

//解析word文档

wordToHtmlConverter.processDocument(wordDocument);

Document htmlDocument = wordToHtmlConverter.getDocument();

File htmlFile = new File(filePath + htmlName);

OutputStream outStream = new FileOutputStream(htmlFile);

DOMSource domSource = new DOMSource(htmlDocument);

StreamResult streamResult = new StreamResult(outStream);

TransformerFactory factory = TransformerFactory.newInstance();

Transformer serializer = factory.newTransformer();

serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");

serializer.setOutputProperty(OutputKeys.INDENT, "yes");

serializer.setOutputProperty(OutputKeys.METHOD, "html");

serializer.transform(domSource, streamResult);

outStream.close();

}

```

```xml

fr.opensagres.xdocreport

fr.opensagres.xdocreport.document

1.0.5

fr.opensagres.xdocreport

org.apache.poi.xwpf.converter.xhtml

1.0.5

org.apache.poi

poi

3.12

org.apache.poi

poi-scratchpad

3.12

```

java docx转html实例_Java实现将word转换为html的方法示例【doc与docx格式】相关推荐

  1. java ip地址查询接口_java获取ip地址与网络接口的方法示例

    java获取ip地址与网络接口的方法示例 发布时间:2020-09-20 23:57:52 来源:脚本之家 阅读:66 作者:骏马金龙 java.net包 大家应该都知道,网络相关对象在java.ne ...

  2. java子类和父类实例_java中父类与子类之间的转换示例

    java中父类与子类之间的转换示例有以下三点: 示例一 父类强制转子类 pre class="brush:php;toolbar:false">Father f = new ...

  3. java编程获取屏幕分辨率_Java编程获取当前屏幕分辨率的方法示例

    本文实例讲述了Java编程获取当前屏幕分辨率的方法.分享给大家供大家参考,具体如下: /** * This class implements the functionality of fetching ...

  4. java类和接口实例_Java定义泛型接口和类的方法实例分析

    本文实例讲述了Java定义泛型接口和类的方法.分享给大家供大家参考,具体如下: 一 点睛 所谓泛型:就是允许在定义类.接口指定类型形参,这个类型形参在将在声明变量.创建对象时确定(即传入实际的类型参数 ...

  5. java 给对象创建实例_Java中创建(实例化)对象的五种方式

    Java中创建(实例化)对象的五种方式1.用new语句创建对象,这是最常见的创建对象的方法. 2.通过工厂方法返回对象,如:String str = String.valueOf(23); 3.运用反 ...

  6. java是什么偏旁部首_Python实现获取汉字偏旁部首的方法示例【测试可用】

    摘要:这篇Python开发技术栏目下的"Python实现获取汉字偏旁部首的方法示例[测试可用]",介绍的技术点是"Python实现.Python.偏旁部首.测试.可用.示 ...

  7. word文档生成系列:doc和docx插入多图

    系列文章目录 第一章:springboot下生成复杂word文档方案 在Word软件里面制作模板 第二章:根据模板导出word,复合格式表格生成.可变列表格生成 第三章:doc和docx插入多图 第四 ...

  8. WORD解析:使用poi解析doc和docx

    1.pom依赖 <!-- https://mvnrepository.com/artifact/org.apache.poi/poi --><dependency><gr ...

  9. java css网页布局实例_java代码例子

    JAVA 类名.方法名(这里面写的是什么)能不能写个代要是类名直接调用的方法,那这个方法就是静态的(static)方法,是不用new出新对象实例就可以直接调用的方法.看下面例子: class A{ p ...

最新文章

  1. circlize包可绘制的几个图形示例
  2. 发现一个很好的工具——VNN
  3. 5.js模式-职责链模式
  4. python实例31[文件夹清理]
  5. PHP 会话 线程 进程,php进程后台调用(多线程/进程)
  6. android tabspec英文自动大写问题,为什么不能在drawable下访问xml文件?所有的
  7. kotlin埋点_GitHub - shajinyang/ilvdo-event-track: 埋点框架
  8. kmean法和dbscan法的直观比较
  9. 第六讲_图像分割Image Segmentation
  10. 如何在软件UI设计中运用格式塔心理学5项法则?
  11. ico图标概述 附生成链接
  12. 怎样调整好炒外汇心态
  13. 指令、微程序、微指令、微命令、微操作之间的联系
  14. 相机标定与棋盘格标定
  15. hadoop103: Permission denied (publickey,gssapi-keyex,gssapi-with-mic,password).
  16. ElementUI 的组件 Switch(开关)如何让文字显示在按钮上
  17. 腾讯云从业者认证报名官网地址
  18. 软件工程 - 需求工程
  19. Few-shots object detection
  20. 亿康先达全球董事会多元化追踪报告显示,进展缓慢且微不足道——有必要采取重大行动

热门文章

  1. ibatisnet 学习手记(1)
  2. 关于.NET编译的目标平台(AnyCPU,x86,x64)
  3. C# Winform实现捕获窗体最小化、最大化、关闭按钮事件的方法,可通过重写WndProc来实现
  4. IOS设置导航栏返回按钮,并添加事件返回主页面
  5. php excel 导入配置,Thinkphp3.2.3整合PHPexcel进行导入导出操作
  6. java zip文件操作,java 关于 zip 文件 的 基本操作
  7. 数据字典怎么写_求职数据分析,项目经验该怎么写
  8. c语言二维数组省略号,LaTex 常用语法
  9. 验证码类,生成验证码
  10. APPium-Xpath,swipe练习