国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > Java > 正文

Java實現將word轉換為html的方法示例【doc與docx格式】

2024-07-14 08:43:56
字體:
來源:轉載
供稿:網友

本文實例講述了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");  }      } } /** * 轉換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教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 柞水县| 台中市| 延川县| 通许县| 仪征市| 东山县| 南汇区| 天柱县| 清徐县| 武冈市| 德庆县| 连城县| 福鼎市| 杭锦后旗| 庆阳市| 甘泉县| 都昌县| 安丘市| 麻城市| 西丰县| 出国| 雷波县| 阆中市| 平原县| 泸水县| 图们市| 海安县| 绍兴市| 本溪市| 栾城县| 互助| 唐河县| 嘉黎县| 疏勒县| 获嘉县| 铜山县| 韶关市| 特克斯县| 日照市| 米易县| 灌阳县|