Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法示例【doc與docx格式】
本文實(shí)例講述了Java實(shí)現(xiàn)將word轉(zhuǎn)換為html的方法。分享給大家供大家參考,具體如下:
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對(duì)象
InputStream in = new FileInputStream(f);
XWPFDocument document = new XWPFDocument(in);
// ) 解析 XHTML配置 (這里設(shè)置IURIResolver來(lái)設(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é)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Java實(shí)現(xiàn)將Word轉(zhuǎn)換成Html的示例代碼
- Java實(shí)現(xiàn)word/pdf轉(zhuǎn)html并在線預(yù)覽
- Java實(shí)現(xiàn)HTML轉(zhuǎn)為Word的示例代碼
- Java 將Word轉(zhuǎn)為HTML的方法
- Java實(shí)現(xiàn)Word/Pdf/TXT轉(zhuǎn)html的實(shí)例代碼
- java實(shí)現(xiàn)在線預(yù)覽--poi實(shí)現(xiàn)word、excel、ppt轉(zhuǎn)html的方法
- java實(shí)現(xiàn)word文件轉(zhuǎn)html文件
- 關(guān)于Java實(shí)現(xiàn)word(docx、doc)轉(zhuǎn)html的完美解決方案
相關(guān)文章
Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法
這篇文章主要介紹了Mybatis通過(guò)Mapper代理連接數(shù)據(jù)庫(kù)的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-11-11
Java的作業(yè)調(diào)度類(lèi)庫(kù)Quartz基本使用指南
這篇文章主要介紹了Java的作業(yè)調(diào)度類(lèi)庫(kù)Quartz基本使用指南,Quartz能夠讓類(lèi)按照指定的計(jì)劃順序執(zhí)行,需要的朋友可以參考下2016-03-03
SpringBoot+SpringSecurity處理Ajax登錄請(qǐng)求問(wèn)題(推薦)
這篇文章主要介紹了SpringBoot+SpringSecurity處理Ajax登錄請(qǐng)求問(wèn)題,本文給大家介紹的非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12
mybatis的映射xml中動(dòng)態(tài)設(shè)置orderby方式
這篇文章主要介紹了mybatis的映射xml中動(dòng)態(tài)設(shè)置orderby方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11

