日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

java docx转html实例_Java实现将word转换为html的方法示例【doc与docx格式】

發布時間:2023/12/15 java 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java docx转html实例_Java实现将word转换为html的方法示例【doc与docx格式】 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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

發布于 2020-6-14|

復制鏈接

摘記: 本文實例講述了Java實現將word轉換為html的方法。分享給大家供大家參考,具體如下:

```java

public static void main(String[] args) throws Exception {

String filePath = "C:/Users/Administrator/Desktop/92個診療方案及臨 ..

本文實例講述了Java實現將word轉換為html的方法。分享給大家供大家參考,具體如下:

```java

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();

}

```

```xml

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 docx转html实例_Java实现将word转换为html的方法示例【doc与docx格式】的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。