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

 更新时间:2019年03月07日 11:44:26   作者:只为学习与记录  
这篇文章主要介绍了Java实现将word转换为html的方法,结合实例形式分析了java针对doc与docx格式文件的相关转换操作技巧,需要的朋友可以参考下

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

 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();
  }

<dependency>
  <groupId>fr.opensagres.xdocreport</groupId>
  <artifactId>fr.opensagres.xdocreport.document</artifactId>
  <version>1.0.5</version>
</dependency>
<dependency> 
  <groupId>fr.opensagres.xdocreport</groupId> 
  <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId> 
  <version>1.0.5</version> 
</dependency>
  <dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi</artifactId>
  <version>3.12</version>
</dependency>
<dependency>
  <groupId>org.apache.poi</groupId>
  <artifactId>poi-scratchpad</artifactId>
  <version>3.12</version>
</dependency>

更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总

希望本文所述对大家java程序设计有所帮助。

相关文章

  • SpringBoot绑定配置文件中变量的四种方式总结

    SpringBoot绑定配置文件中变量的四种方式总结

    当在Spring Boot中需要绑定配置文件中的变量时,可以使用以下注解:@PropertySourc,@Value,@Environment,@ConfigurationProperties,具体实现代码示例文中讲解的非常详细,需要的朋友可以参考下
    2023-11-11
  • Java实现对比两个实体类字段变化的示例详解

    Java实现对比两个实体类字段变化的示例详解

    我们在工作中,可能会在日志中记录数据的变化情况或者在公共处理的数据增加一个日志页面,我们可以根据CompareUtils工具类比较数据前后发生了怎样的变化,下面我们就来看看具体实现吧
    2024-03-03
  • 一篇文章带你入门Java方法详解

    一篇文章带你入门Java方法详解

    这篇文章主要介绍了简单了解Java方法的定义和使用实现详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2021-08-08
  • Java集合中的LinkedHashMap使用解析

    Java集合中的LinkedHashMap使用解析

    这篇文章主要介绍了Java集合中的LinkedHashMap使用解析,LinkedHashMap是继承于HashMap的,所以它的很多属性和方法都是HashMap中的,那么它是怎么实现有序存储的呢,需要的朋友可以参考下
    2023-12-12
  • 了解java中的Clojure如何抽象并发性和共享状态

    了解java中的Clojure如何抽象并发性和共享状态

    Clojure是一种运行在Java平台上的 Lisp 方言,Lisp是一种以表达性和功能强大著称的编程语言,但人们通常认为它不太适合应用于一般情况,而Clojure的出现彻底改变了这一现状。,需要的朋友可以参考下
    2019-06-06
  • Java两个变量的互换(不借助第3个变量)具体实现方法

    Java两个变量的互换(不借助第3个变量)具体实现方法

    这篇文章主要介绍了Java两个变量的互换(不借助第3个变量)具体实现方法,需要的朋友可以参考下
    2014-02-02
  • Java中I/O流读取数据不完整的问题解决

    Java中I/O流读取数据不完整的问题解决

    本文主要介绍了ava中I/O流读取数据不完整的问题,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-05-05
  • Spring中ApplicationEventPublisher发布订阅模式的实现

    Spring中ApplicationEventPublisher发布订阅模式的实现

    本文主要介绍了Spring中ApplicationEventPublisher发布订阅模式的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-07-07
  • 解决SpringCloud Gateway配置自定义路由404的坑

    解决SpringCloud Gateway配置自定义路由404的坑

    这篇文章主要介绍了解决SpringCloud Gateway配置自定义路由404的坑,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-09-09
  • 非常全面的Java异常处理(全文干货,值得收藏)

    非常全面的Java异常处理(全文干货,值得收藏)

    这篇文章主要给大家介绍了非常全面的Java异常处理的相关资料,全文干货,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11

最新评论