欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

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

时间:2022-02-18 09:15:50|栏目:JAVA代码|点击:

本文实例讲述了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程序设计有所帮助。

上一篇:java实现随机验证码图片生成

栏    目:JAVA代码

下一篇:基于idea 的 Java中的get/set方法之优雅的写法

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

本文地址:http://www.codeinn.net/misctech/193803.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有