Required maven dependencies are compiled and must be versioned with consistent:<!--https://mvnrepository.com/artifact/fr.opensagres.xdocreport/ Org.apache.poi.xwpf.converter.core-<dependency> <groupid>fr.opensagres.xdocreport</groupid > <artifactId>org.apache.poi.xwpf.converter.core</artifactId> <version>1.0.5</version > </dependency> <!--https://mvnrepository.com/artifact/fr.opensagres.xdocreport/ Org.apache.poi.xwpf.converter.xhtml-<dependency> <groupid>fr.opensagres.xdocreport</groupid > <artifactId>org.apache.poi.xwpf.converter.xhtml</artifactId> <version>1.0.5</version > </dependency><!--version is consistent with--<!--https://mvnrepository.com/artifact/org.apache.commons/ Commons-io-<dependency> <groupId>org.apache.commons</groupId> <artifactId> commons-io</artifactid> <version>1.3.2</version> </dependency> <dependency> < Groupid>org.apache.poi</groupid> <aRtifactid>poi-scratchpad</artifactid> <version>3.17</version> </dependency> <!-- HTTPS://MVNREPOSITORY.COM/ARTIFACT/ORG.APACHE.COMMONS/COMMONS-COLLECTIONS4-<dependency> <groupid >org.apache.commons</groupId> <artifactId>commons-collections4</artifactId> <version> 4.0</version> </dependency> <dependency> <groupId>org.apache.poi</groupId> < Artifactid>poi</artifactid> <version>3.17</version> </dependency> <!--< Dependency> <groupId>org.apache.poi</groupId> <artifactid>poi-ooxml-schemas</artifactid > <version>3.16</version> </dependency> <!--https://mvnrepository.com/artifact/ Org.apache.xmlbeans/xmlbeans-<dependency> <groupId>org.apache.xmlbeans</groupId> < artifactid>xmlbeans</artifactid> <version>2.6.0</version> </dependency> <dependency > ≪groupid>org.apache.poi</groupid> <artifactId>poi-ooxml</artifactId> <version>3.14 </version> </dependency><!--The package referenced by the POI to keep the version number consistent, ClassNotFoundException will also appear: Org.apache.poi.wp.usermodel.Paragraph This error-<!--https://mvnrepository.com/artifact/org.apache.commons/ Commons-lang3-<dependency> <groupId>org.apache.commons</groupId> <artifactId> Commons-lang3</artifactid> <version>3.4</version> </dependency> Package Com.zyhao.openec.excel.utils;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileInputStream;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;
Import Java.io.InputStream;
Import Java.io.OutputStreamWriter;
Import java.util.List;
Import Javax.xml.parsers.DocumentBuilderFactory;
Import Javax.xml.transform.OutputKeys;
Import Javax.xml.transform.Transformer;
Import Javax.xml.transform.TransformerFactory;
Import Javax.xml.transform.dom.DOMSource;
Import Javax.xml.transform.stream.StreamResult;
Import Org.apache.commons.io.output.ByteArrayOutputStream;
Import org.apache.poi.hwpf.HWPFDocument;
Import Org.apache.poi.hwpf.converter.PicturesManager;
Import Org.apache.poi.hwpf.converter.WordToHtmlConverter;
Import Org.apache.poi.hwpf.usermodel.Picture;
Import Org.apache.poi.hwpf.usermodel.PictureType;
Import Org.apache.poi.xwpf.converter.core.FileImageExtractor;
Import Org.apache.poi.xwpf.converter.core.IURIResolver;
Import Org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
Import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
Import org.apache.poi.xwpf.usermodel.XWPFDocument;
Import org.w3c.dom.Document;
public class Wordtohtml {
public static void Main (string[] args) {
try {
Wordtohtml ("E:\me\2.docx", "E:\me\", "123.html");
Wordtohtml ("E:\me\2.doc", "E:\me\", "12.html");
} catch (Exception e) {
E.printstacktrace ();
}
}
public static void wordtohtml (String wordpath,string htmlpath,string newfilename) throws Exception {CONVERT2HT ML (Wordpath, Htmlpath, newfilename);} public static void WriteFile (string content, String path) throws Exception {FileOutputStream fos = null; BufferedWriter bw = NULL; try {File File = new file (path); FOS = new FileOutputStream (file); BW = new BufferedWriter (new OutputStreamWriter (FOS)); Bw.write (content); } catch (FileNotFoundException Fnfe) {fnfe.printstacktrace (); } catch (IOException IoE) {ioe.printstacktrace (); } finally {try {if (bw! = NULL) bw.close (); if (fos! = null) fos.close (); } catch (IOException IE) {}}}/** * Convert Word to HTML * support. doc and. docx * @param filename * @param outputfilepath HTML storage path * @param newfilename HTML name * @tHrows Exception */public static void convert2html (String fileName, String outputfilepath,string newfilename) t Hrows Exception {String substring = filename.substring (Filename.lastindexof (".") +1); Bytearrayoutputstream out = new Bytearrayoutputstream (); /** * word2007 and word2003 are constructed differently, the former is constructed in XML, and the latter is constructed in the DOM tree. * The suffix of the file is also different, the former suffix is. docx, and the latter suffix is. doc * corresponding, APACHE.POI provides different implementation classes. */if ("docx". Equals (substring)) {
WriteFile (New String ("
//step 1 : load DOCX into XWPFDocument InputStream inputStream = new FileInputStream(new File(fileName)); XWPFDocument document = new XWPFDocument(inputStream); //step 2 : prepare XHTML options final String imageUrl = ""; XHTMLOptions options = XHTMLOptions.create(); options.setExtractor(new FileImageExtractor(new File(outPutFilePath + imageUrl))); options.setIgnoreStylesIfUnused(false); options.setFragment(true); options.URIResolver(new IURIResolver() {
@Override rewrite the method, plus this error, you see what the problem
public string Resolve (string uri) {
return imageUrl + URI;
}
});
Step 3:convert xwpfdocument to XHTML xhtmlconverter.getinstance (). Convert (document, out, options); }else{hwpfdocument worddocument = new Hwpfdocument (new FileInputStream (FileName));//wordtohtmlutils.loaddoc (New Fi Leinputstream (Inputfile)); Wordtohtmlconverter wordtohtmlconverter = new Wordtohtmlconverter (Documentbuilderfactory.newinstance () . Newdocumentbuilder (). NewDocument ()); Wordtohtmlconverter.setpicturesmanager (New Picturesmanager () {public String savepicture (byt e[] Content, PictureType PictureType, String suggestedname, float widthinc Hes, float heightinches) {return suggestedname; } } ); Wordtohtmlconverter.processdocument (worddocument); Save Pictures List pics=worddocument.getpicturestable (). Getallpictures (); if (Pics!=null&&!pics.isempty ()) {for (Int. i=0;i<pics.size (); i++) {Picture pic = (P icture) Pics.get (i); System.out.println (); try {pic.writeimagecontent (new FileOutputStream (Outputfilepath + pi C.suggestfullfilename ())); } catch (FileNotFoundException e) {e.printstacktrace (); }}} Document HTMLDocument = Wordtohtmlconverter.getdocument (); Domsource Domsource = new Domsource (htmldocument); Streamresult Streamresult = new Streamresult (out); Transformerfactory tf = Transformerfactory.newinstance (); This should be converted to XML Transformer serializer = Tf.newtransformer (); Serializer.setoutputproperty (outputkeys.encoding, "utf-8"); Serializer.setoutputproperty (outputkeys.indent, "yes"); SerIalizer.setoutputproperty (Outputkeys.method, "html"); Serializer.transform (Domsource, Streamresult); } out.close (); WriteFile (New String (Out.tobytearray ()), outputfilepath+newfilename); }
}
Word Export HTML Implementation online preview