本文實(shí)例講述了Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法。分享給大家供大家參考,具體如下:
創(chuàng)新互聯(lián)建站專業(yè)為企業(yè)提供柳州網(wǎng)站建設(shè)、柳州做網(wǎng)站、柳州網(wǎng)站設(shè)計(jì)、柳州網(wǎng)站制作等企業(yè)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計(jì)與制作、柳州企業(yè)網(wǎng)站模板建站服務(wù),十載柳州做網(wǎng)站經(jīng)驗(yàn),不只是建網(wǎng)站,更提供有價(jià)值的思路和整體網(wǎng)絡(luò)服務(wù)。
public static void main(String[] args) throws Exception { String filePath = "C:/Users/Administrator/Desktop/92個(gè)診療方案及臨床路徑/"; 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"); } } } /** * 轉(zhuǎn)換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配置 (這里設(shè)置IURIResolver來設(shè)置圖片存放的目錄) 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轉(zhuǎn)換成XHTML OutputStream out = new FileOutputStream(new File(filePath + htmlName)); XHTMLConverter.getInstance().convert(document, out, options); } /** * 轉(zhuǎn)換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(); }
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
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計(jì)有所幫助。