Java實現(xiàn)將word轉(zhuǎn)換為html的方法示例【doc與docx格式】
本文實例講述了Java實現(xiàn)將word轉(zhuǎn)換為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");
}
}
}
/**
* 轉(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();
}
<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>
更多關(guān)于java算法相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對大家java程序設(shè)計有所幫助。
- Java實現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼
- Java實現(xiàn)word/pdf轉(zhuǎn)html并在線預(yù)覽
- Java實現(xiàn)HTML轉(zhuǎn)為Word的示例代碼
- Java 將Word轉(zhuǎn)為HTML的方法
- Java實現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實例代碼
- java實現(xiàn)在線預(yù)覽--poi實現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- java實現(xiàn)word文件轉(zhuǎn)html文件
- 關(guān)于Java實現(xiàn)word(docx、doc)轉(zhuǎn)html的完美解決方案
相關(guān)文章
SpringBoot綁定配置文件中變量的四種方式總結(jié)
當在Spring Boot中需要綁定配置文件中的變量時,可以使用以下注解:@PropertySourc,@Value,@Environment,@ConfigurationProperties,具體實現(xiàn)代碼示例文中講解的非常詳細,需要的朋友可以參考下2023-11-11
了解java中的Clojure如何抽象并發(fā)性和共享狀態(tài)
Clojure是一種運行在Java平臺上的 Lisp 方言,Lisp是一種以表達性和功能強大著稱的編程語言,但人們通常認為它不太適合應(yīng)用于一般情況,而Clojure的出現(xiàn)徹底改變了這一現(xiàn)狀。,需要的朋友可以參考下2019-06-06
Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法
這篇文章主要介紹了Java兩個變量的互換(不借助第3個變量)具體實現(xiàn)方法,需要的朋友可以參考下2014-02-02
Java中I/O流讀取數(shù)據(jù)不完整的問題解決
本文主要介紹了ava中I/O流讀取數(shù)據(jù)不完整的問題,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
Spring中ApplicationEventPublisher發(fā)布訂閱模式的實現(xiàn)
本文主要介紹了Spring中ApplicationEventPublisher發(fā)布訂閱模式的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
解決SpringCloud Gateway配置自定義路由404的坑
這篇文章主要介紹了解決SpringCloud Gateway配置自定義路由404的坑,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09

