本文实例讲述了Java实现将词转换为html的方法。分享给大家供大家参考,具体如下:
公共静态void main (String [] args){抛出异常 字符串filePath=" C:/用户/管理员/桌面/92个诊疗方案及临床路径/? 文件文件=新文件(filePath); 文件[]文件=file.listFiles (); 字符串名称=零; (文件file2:文件){ thread . sleep (500); name=file2.getName ()。substring (0, file2.getName () .lastIndexOf (“。”); System.out.println (file2.getName ()); 如果(file2.getName () .endsWith (“。docx”) | | file2.getName () .endsWith (“。docx”)) { CaseHtm。多克斯(filePath file2.getName(),名字+ " . htm "); 其他}{ CaseHtm。阿霉素(filePath file2.getName(),名字+ " . htm "); } } }/* * *转换多克斯 * @param filePath * @param文件名 * @param htmlName * @throws例外 */公共静态孔隙多克斯(文件名字符串filePath,字符串,字符串htmlName){抛出异常 最终字符串文件=filePath +文件名; 文件f=新文件(文件);//)加载词文档生成XWPFDocument对象 InputStream=新FileInputStream (f); XWPFDocument文档=new XWPFDocument(的);//)解析XHTML配置(这里设置IURIResolver来设置图片存放的目录) 文件imageFolderFile=新文件(filePath); XHTMLOptions选项=XHTMLOptions.create ()。URIResolver(新FileURIResolver (imageFolderFile)); 选项。setExtractor(新FileImageExtractor (imageFolderFile)); options.setIgnoreStylesIfUnused(假); options.setFragment(真正的);//)将XWPFDocument转换成XHTML OutputStream=新FileOutputStream(新文件(filePath + htmlName)); XHTMLConverter.getInstance ()。把(文件,选项); }/* * *转换医生 * @param filePath * @param文件名 * @param htmlName * @throws例外 */公共静态空虚阿霉素(文件名字符串filePath,字符串,字符串htmlName){抛出异常 最终字符串文件=filePath +文件名; InputStream输入=new FileInputStream(新文件(文件); HWPFDocument wordDocument=new HWPFDocument(输入); .newDocumentBuilder WordToHtmlConverter WordToHtmlConverter=new WordToHtmlConverter (DocumentBuilderFactory.newInstance () () .newDocument ());//解析字文档 wordToHtmlConverter.processDocument (wordDocument); 文档htmlDocument=wordToHtmlConverter.getDocument (); 文件htmlFile=新文件(filePath + htmlName); OutputStream outStream=new FileOutputStream (htmlFile); DOMSource DOMSource=new DOMSource (htmlDocument); StreamResult StreamResult=new StreamResult (outStream); TransformerFactory工厂=TransformerFactory.newInstance (); 变压器序列化器=factory.newTransformer (); serializer.setOutputProperty (OutputKeys。编码,“utf - 8”); serializer.setOutputProperty (OutputKeys。缩进:“是的”); serializer.setOutputProperty (OutputKeys。方法,“html”); 序列化器。变换(domSource streamResult); outStream.close (); } >之前& lt; dependency> & lt; groupId> fr.opensagres.xdocreport & lt; artifactId> fr.opensagres.xdocreport.document & lt; version> 1.0.5 & lt;/dependency> & lt; dependency> & lt; groupId> fr.opensagres.xdocreport & lt; artifactId> org.apache.poi.xwpf.converter.xhtml & lt; version> 1.0.5 & lt;/dependency> & lt; dependency> & lt; groupId> org.apache.poi & lt; artifactId> poi & lt; version> 3.12 & lt;/version> & lt;/dependency> & lt; dependency> & lt; groupId> org.apache.poi & lt; artifactId> poi-scratchpad & lt; version> 3.12 & lt;/version> & lt;/dependency> >之前更多关于java算法相关内容感兴趣的读者可查看本站专题:《java文件与目录操作技巧汇总》、《java数据结构与算法教程》、《java操作DOM节点技巧总结》和《java缓存操作技巧汇总》
希望本文所述对大家java程序设计有所帮助。
Java实现将词转换为html的方法示例【医生与多克斯格式】