使用poi将word转换为html,支持doc,docx,转换后可以保持图片、样式。
1.导入
<dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>3.14</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-scratchpad</artifactId> <version>3.14</version> </dependency> <dependency> <groupId> org.apache.poi </groupId> <ArtifActId> poi-ooxml </artifactid> <版本> <版本> 3.14 </distrifency> </deverency> <dependency> <groupId> <groupId> fr.opensagres.xdocReport.xdocReport </groupId> </groupId> <groupId> org.apache.poi </groupId> <ARTIFACTID> poi-ooxml-schemas </artifactid> </artifactid> <版本> 3.14 </version> </dependency> <dependency> <groudency> <groupId> org.apache.poi </groupId>
2.转换代码
导入org.apache.poi.hwpf.hwpfdocument;导入org.apache.poi.hwpf.converter.wordtohtmlConverter;导入org.apache.poi.xwpf.converter.core.basicuriresolver;导入org.apache.poi.xwpf.converter.core.fileimageExtractor;导入org.apache.poi.xwpf.converter.xhtml.xhtmlConverter;导入org.apache.poi.xwpf.converter.xhtml.xhtmloptions;导入org.apache.poi.xwpf.usermodel.xwpfdocument;导入org.w3c.dom.document;导入javax.xml.parsers.documentBuilderFactory;导入javax.xml.transform.outputkeys;导入javax.xml.transform.transformer;导入javax.xml.transform.transformerfactory;导入javax.xml.transform.domsource;导入javax.xml.transform.stream.streamresult;导入java.io.file;导入java.io.io.fileinputstream;导入java.io.io.fileoutputstream;导入java.io.OutputStreamWriter;公共类测试{// doc转换为html void doctohtml()引发异常{字符串sourcefilename =“ c://doc//test.doc”;字符串targetFileName =“ c://html//test.html”;字符串ImagePath =“ C:// html // image //”; hwpfdocument worddocument = new hwpfdocument(new fileInputStream(sourceFileName));文档= documentBuilderFactory.NewInstance()。newDocumentBuilder()。newDocument(); WordTohtmlConverter WordTohtmlConverter = new WordTohtmlConverter(document); //保存图片,并返回图片的相对路径wordtohtmlConverter.setPicturesManager(((content,picturetype,name,width,height)) - > {trif(fileOutputstream out = new fileOutputStream(imagepathstr + name)){out.write(out.write(prons.write(content)) WordToHtmlConverter.ProcessDocument(WordDocument);文档htmldocument = wordtohtmlConverter.getDocument(); domsource domsource = new domsource(htmldocument); StreamResult StreamResult = new StreamResult(new File(targetFileName)); TransformerFactory TF = TransformerFactory.NewInstance();变压器序列化器= tf.newtransformer(); serializer.SetOutputProperty(outputKeys.Coding,“ UTF-8”); serializer.setOutputProperty(outputkeys.indent,“ yes”); serializer.setOutputProperty(outputKeys.Method,“ html”); serializer.transform(domsource,stream result); } // docx转换为html public void docxtohtml()抛出异常{字符串sourcefileName =“ d://ac//00.docx”;字符串targetFileName =“ d://ac//test.html”;字符串ImagePath =“ d:// ac // image //”; outputStreamWriter outputStreamWriter = null;尝试{xwpfdocument document = new xwpfdocument(new fileInputStream(sourceFileName)); xhtmloptions选项= xhtmloptions.create(); //存放图片的文件夹options.setExtractor(new FileImageExtractor(new File(imagePathStr))); //html中图片的路径选项。 outputStreamWriter = new UppotStreamWriter(new FileOutputStream(targetFileName),“ UTF-8”); xhtmlConverter xhtmlConverter =(xhtmlConverter)xhtmlConverter.getInstance(); xhtmlConverter.convert(文档,outputStreamWriter,options); }最后{if(outputStreamWriter!= null){utputStreamWriter.close(); }}}}演示地址:https://www.xiaoyun.studio/app/preview.html
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持武林网。,也希望大家多多支持武林网。