您好,登錄后才能下訂單哦!
小編給大家分享一下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(); }
pom.xml配置:
<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將word轉換為html的示例的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。