Java实现将word转换为html的方法示例【doc与docx格式】
本文实例讲述了Java实现将word转换为html的方法。分享给大家供大家参考,具体如下:
publicstaticvoidmain(String[]args)throwsException{
StringfilePath="C:/Users/Administrator/Desktop/92个诊疗方案及临床路径/";
Filefile=newFile(filePath);
File[]files=file.listFiles();
Stringname=null;
for(Filefile2: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
*@paramfilePath
*@paramfileName
*@paramhtmlName
*@throwsException
*/
publicstaticvoiddocx(StringfilePath,StringfileName,StringhtmlName)throwsException{
finalStringfile=filePath+fileName;
Filef=newFile(file);
//)加载word文档生成XWPFDocument对象
InputStreamin=newFileInputStream(f);
XWPFDocumentdocument=newXWPFDocument(in);
//)解析XHTML配置(这里设置IURIResolver来设置图片存放的目录)
FileimageFolderFile=newFile(filePath);
XHTMLOptionsoptions=XHTMLOptions.create().URIResolver(newFileURIResolver(imageFolderFile));
options.setExtractor(newFileImageExtractor(imageFolderFile));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
//)将XWPFDocument转换成XHTML
OutputStreamout=newFileOutputStream(newFile(filePath+htmlName));
XHTMLConverter.getInstance().convert(document,out,options);
}
/**
*转换doc
*@paramfilePath
*@paramfileName
*@paramhtmlName
*@throwsException
*/
publicstaticvoiddox(StringfilePath,StringfileName,StringhtmlName)throwsException{
finalStringfile=filePath+fileName;
InputStreaminput=newFileInputStream(newFile(file));
HWPFDocumentwordDocument=newHWPFDocument(input);
WordToHtmlConverterwordToHtmlConverter=newWordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
//解析word文档
wordToHtmlConverter.processDocument(wordDocument);
DocumenthtmlDocument=wordToHtmlConverter.getDocument();
FilehtmlFile=newFile(filePath+htmlName);
OutputStreamoutStream=newFileOutputStream(htmlFile);
DOMSourcedomSource=newDOMSource(htmlDocument);
StreamResultstreamResult=newStreamResult(outStream);
TransformerFactoryfactory=TransformerFactory.newInstance();
Transformerserializer=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
更多关于java算法相关内容感兴趣的读者可查看本站专题:《Java文件与目录操作技巧汇总》、《Java数据结构与算法教程》、《Java操作DOM节点技巧总结》和《Java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。