使用poi将wordhtml、支持doc、docx、转换后可以保持图片、样式。
1. maven包
<Dependency> groupId> org.apache.poi </groupid> <artifactid> poi </artifactid> <version> 3.14 </version> </dependency> <dependency> groupid> org.apache.poi </groupId> <artifactid> poi-scratchpad </artifactid> <バージョン> <groupid> org.apache.poi </groupid> <artifactid> poi-ooxml </artifactid> <version> 3.14 </version> </dependency> <dependency> <groupid> fr.opensagres.xdocreport </groupId> <artifactid> xdocreport </artifactid> </</</</</</</</< <groupid> org.apache.poi </groupid> <artifactid> poi-ooxml-schemas </artifactid> <bersion> 3.14 </version> </dependency> <dependency> <groupid> org.apache.poi </groupid> <artifactid> ooxml-schemas </artifactid> </</</</<
2.转换代码
Import org.apache.poi.hwpf.hwpfdocument; org.apache.poi.hwpf.converter.wordtohtmlconverterをインポートします。 org.apache.poi.xwpf.converter.core.basicurireSolverをインポートします。 Import org.apache.poi.xwpf.converter.core.fileimageextractor; import org.apache.poi.xwpf.converter.xhtml.xhtmlconverter; import org.apache.poi.xwpf.converter.xhtml.xhtmloptions; org.apache.poi.xwpf.usermodel.xwpfdocumentをインポートします。 Import 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.fileinputStreamをインポートします。 java.io.fileoutputStreamをインポートします。 java.io.outputStreamWriterをインポートします。 public class test {//doc转换为htmlvoid doctohtml()throws exception {string sourcefilename = "c://doc//test.doc"; string targetFileName = "c://html//test.html"; string imagepathstr = "c:// html // image //"; hwpfdocument worddocument = new hwpfdocument(new fileinputStream(sourceFileName)); document document = documentbuilderfactory.newinstance()。newdocumentbuilder()。newdocument(); wordtohtmlconverter wordtohtmlconverter = new wordtohtmlconverter(document); //保存图片、并返回图片的相对路径wordtohtmlconverter.setpicturesmanager((content、picturetype、name、width、height) - > {try(fileoutputStream out = new fileoutputStream(ImagePathstr + name){out.write(content);} catch(Exceance e){e.printstacktrace(e.printstacktrace(}; wordtohtmlconverter.processdocument(worddocument);ドキュメントhtmldocument = wordtohtmlconverter.getDocument(); domsource domsource = new domsource(htmldocument); StreamResult StreamResult = new StreamResult(new file(targetFileName)); TransformerFactory TF = TransformerFactory.NewInstance(); Transformer serializer = tf.newtransformer(); serializer.setOutputProperty(outputKeys.encoding、 "utf-8"); serializer.setOutputProperty(outputKeys.indent、 "yes"); Serializer.setOutputProperty(outputKeys.Method、 "HTML"); serializer.transform(domsource、streamResult); } // docx转换为HTMLpublic void docxtohtml()throws exception {string sourcefilename = "d://ac//00.docx"; string targetfilename = "d://ac//test.html";文字列imagepathstr = "d:// ac // image //"; outputStreamWriter outputStreamWriter = null; try {xwpfdocument document = new xwpfdocument(new FileInputStream(sourceFileName)); xhtmloptions options = xhtmloptions.create(); // options.setextractor(new fileimageExtractor(new file(ImagePathstr))); // html options.urireSolver(new BasicurireSolver( "Image")); outputStreamWriter = new outputStreamWriter(new FileOutputStream(TargetFileName)、 "UTF-8"); XhtmlConverter xhtmlConverter =(xhtmlConverter)xhtmlConverter.getInstance(); xhtmlconverter.convert(document、outputStreamWriter、options); }最後に{if(outputStreamWriter!= null){outputStreamWriter.close(); }}}演示地址:https://www.xiaoyun.studio/app/preview.html
以上就是本文的全部内容、希望对大家的学习有所帮助、也希望大家多多支持武林网。