java将word转换为html

阳光穿透心脏的1/2处 2022-06-02 07:59 348阅读 0赞
  1. public static void main(String[] args) throws Exception {
  2. String filePath = "C:/Users/Administrator/Desktop/92个诊疗方案及临床路径/";
  3. File file = new File(filePath);
  4. File[] files = file.listFiles();
  5. String name = null;
  6. for (File file2 : files) {
  7. Thread.sleep(500);
  8. name = file2.getName().substring(0, file2.getName().lastIndexOf("."));
  9. System.out.println(file2.getName());
  10. if (file2.getName().endsWith(".docx") || file2.getName().endsWith(".DOCX")) {
  11. CaseHtm.docx(filePath ,file2.getName(),name +".htm");
  12. }else{
  13. CaseHtm.dox(filePath ,file2.getName(),name +".htm");
  14. }
  15. }
  16. }
  17. /**
  18. * 转换docx
  19. * @param filePath
  20. * @param fileName
  21. * @param htmlName
  22. * @throws Exception
  23. */
  24. public static void docx(String filePath ,String fileName,String htmlName) throws Exception{
  25. final String file = filePath + fileName;
  26. File f = new File(file);
  27. // ) 加载word文档生成 XWPFDocument对象
  28. InputStream in = new FileInputStream(f);
  29. XWPFDocument document = new XWPFDocument(in);
  30. // ) 解析 XHTML配置 (这里设置IURIResolver来设置图片存放的目录)
  31. File imageFolderFile = new File(filePath);
  32. XHTMLOptions options = XHTMLOptions.create().URIResolver(new FileURIResolver(imageFolderFile));
  33. options.setExtractor(new FileImageExtractor(imageFolderFile));
  34. options.setIgnoreStylesIfUnused(false);
  35. options.setFragment(true);
  36. // ) 将 XWPFDocument转换成XHTML
  37. OutputStream out = new FileOutputStream(new File(filePath + htmlName));
  38. XHTMLConverter.getInstance().convert(document, out, options);
  39. }
  40. /**
  41. * 转换doc
  42. * @param filePath
  43. * @param fileName
  44. * @param htmlName
  45. * @throws Exception
  46. */
  47. public static void dox(String filePath ,String fileName,String htmlName) throws Exception{
  48. final String file = filePath + fileName;
  49. InputStream input = new FileInputStream(new File(file));
  50. HWPFDocument wordDocument = new HWPFDocument(input);
  51. WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
  52. //解析word文档
  53. wordToHtmlConverter.processDocument(wordDocument);
  54. Document htmlDocument = wordToHtmlConverter.getDocument();
  55. File htmlFile = new File(filePath + htmlName);
  56. OutputStream outStream = new FileOutputStream(htmlFile);
  57. DOMSource domSource = new DOMSource(htmlDocument);
  58. StreamResult streamResult = new StreamResult(outStream);
  59. TransformerFactory factory = TransformerFactory.newInstance();
  60. Transformer serializer = factory.newTransformer();
  61. serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
  62. serializer.setOutputProperty(OutputKeys.INDENT, "yes");
  63. serializer.setOutputProperty(OutputKeys.METHOD, "html");
  64. serializer.transform(domSource, streamResult);
  65. outStream.close();
  66. }
  67. <dependency>
  68. <groupId>fr.opensagres.xdocreport</groupId>
  69. <artifactId>fr.opensagres.xdocreport.document</artifactId>
  70. <version>1.0.5</version>
  71. </dependency>
  72. <dependency>
  73. <groupId>fr.opensagres.xdocreport</groupId>
  74. <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId>
  75. <version>1.0.5</version>
  76. </dependency>
  77. <dependency>
  78. <groupId>org.apache.poi</groupId>
  79. <artifactId>poi</artifactId>
  80. <version>3.12</version>
  81. </dependency>
  82. <dependency>
  83. <groupId>org.apache.poi</groupId>
  84. <artifactId>poi-scratchpad</artifactId>
  85. <version>3.12</version>
  86. </dependency>

发表评论

表情:
评论列表 (有 0 条评论,348人围观)

还没有评论,来说两句吧...

相关阅读