栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java实现将word转换为html的方法示例

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java实现将word转换为html的方法示例

本文实例讲述了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");
  }
  
    }
 }
 
 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);
 }
 
 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();
  }


  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算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》

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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/138875.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号