Java converts word to HTML

Source: Internet
Author: User
Tags uuid

I am here Maven project, just configure in the resource file, will automatically download AR package

Configuring in Pox.xml

<!--word goes to HTML https://mvnrepository.com/artifact/org.apache.poi/poi-scratchpad -            <Dependency>                <groupId>Org.apache.poi</groupId>                <Artifactid>Poi-scratchpad</Artifactid>                <version>3.17</version>            </Dependency>                                <!--Https://mvnrepository.com/artifact/org.apache.poi/poi-ooxml -            <Dependency>                <groupId>Org.apache.poi</groupId>                <Artifactid>Poi-ooxml</Artifactid>                <version>3.17</version>            </Dependency>                            <!--HTTPS://MVNREPOSITORY.COM/ARTIFACT/FR.OPENSAGRES.XDOCREPORT/FR.OPENSAGRES.XDOCREPORT.CONVERTER.DOCX.XWPF  -            <Dependency>                <groupId>Fr.opensagres.xdocreport</groupId>                <Artifactid>Fr.opensagres.xdocreport.converter.docx.xwpf</Artifactid>                <version>2.0.1</version>            </Dependency>

Java code

 PackageCom.lmt.service.file;ImportJava.io.ByteArrayInputStream;ImportJava.io.ByteArrayOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.FileWriter;Importjava.io.IOException;ImportJava.io.InputStream;ImportJava.util.UUID;Importjavax.xml.parsers.DocumentBuilderFactory;Importjavax.xml.parsers.ParserConfigurationException;ImportJavax.xml.transform.OutputKeys;ImportJavax.xml.transform.Transformer;Importjavax.xml.transform.TransformerException;Importjavax.xml.transform.TransformerFactory;ImportJavax.xml.transform.dom.DOMSource;ImportJavax.xml.transform.stream.StreamResult;Importorg.apache.poi.hwpf.HWPFDocument;ImportOrg.apache.poi.hwpf.converter.PicturesManager;ImportOrg.apache.poi.hwpf.converter.WordToHtmlConverter;ImportOrg.apache.poi.hwpf.usermodel.PictureType;Importorg.apache.poi.util.IOUtils;Importorg.apache.poi.xwpf.usermodel.XWPFDocument;ImportOrg.slf4j.Logger;Importorg.slf4j.LoggerFactory;Importorg.springframework.beans.factory.annotation.Autowired;Importorg.springframework.stereotype.Component;ImportOrg.springframework.web.multipart.MultipartFile;Importorg.w3c.dom.Document;ImportFr.opensagres.poi.xwpf.converter.core.ImageManager;ImportFr.opensagres.poi.xwpf.converter.xhtml.XHTMLConverter;Importfr.opensagres.poi.xwpf.converter.xhtml.XHTMLOptions; @Component Public classwordtohtml {Private Static FinalLogger Logger = Loggerfactory.getlogger (wordtohtml.class); @AutowiredPrivateParsefile Parsefile;  PublicFile Convert (multipartfile file) {String filename=File.getoriginalfilename (); String suffix=filename.substring (Filename.lastindexof (".")); String NewName=Uuid.randomuuid (). toString (); //TODO needs to be saved in a new locationFile Convfile =NewFile ("d:/test/" + NewName +suffix); FileOutputStream Fos=NULL; Try{convfile.createnewfile (); FOS=NewFileOutputStream (Convfile);        Fos.write (File.getbytes ()); } Catch(IOException ex) {Logger.error ("Error uploading File!" ", ex); return NULL; } finally{ioutils.closequietly (FOS); }                //Enter the folder where the file name is located//Plus backslashString parentdirectory =convfile.getparent (); if(!parentdirectory.endswith ("\ \") ) {parentdirectory= parentdirectory + "\ \"; }                if(Filename.endswith (". docx")) {            returnDocxconvert (Parentdirectory, Convfile.getabsolutepath (), newName); } Else if(Filename.endswith (". Doc")) {            returnDocconvert (Parentdirectory, Convfile.getabsolutepath (), newName); } Else{logger.error ("Unsupported file Format! "); return NULL; }    }            PrivateFile Docxconvert (String parentdirectory, String filename,string newName) {Try{xwpfdocument document=NewXwpfdocument (Newfileinputstream (filename)); Xhtmloptions Options= Xhtmloptions.create (). Setimagemanager (NewImagemanager (NewFile (Parentdirectory), Uuid.randomuuid (). toString ()). Indent (4); FileOutputStream out=NewFileOutputStream (NewFile (parentdirectory + newname+ ". html"));            Xhtmlconverter.getinstance (). Convert (document, out, options); return NewFile (parentdirectory + newname+ ". html"); } Catch(IOException ex) {Logger.error ("Word Conversion Error!" ", ex); return NULL; }            }            PrivateFile Docconvert (String parentdirectory, String filename,string newName) {Try{hwpfdocument document=NewHwpfdocument (Newfileinputstream (filename)); Wordtohtmlconverter Wordtohtmlconverter=NewWordtohtmlconverter (Documentbuilderfactory.newinstance (). Newdocumentbuilder ()                        . NewDocument ()); //Converter The image is not processed by default, you need to manually download the picture and embed it in HTMLWordtohtmlconverter.setpicturesmanager (NewPicturesmanager () {@Override PublicString SavePicture (byte[] bytes, PictureType picturetype, String s,floatVfloatv1) {String Imagefilename= Parentdirectory + ""; String Identity=Uuid.randomuuid (). toString (); File ImageFile=NewFile (Imagefilename, identity+s);                        Imagefile.getparentfile (). Mkdirs (); InputStream in=NULL; FileOutputStream out=NULL; Try{ in=Newbytearrayinputstream (bytes); out=NewFileOutputStream (ImageFile);                        Ioutils.copy (in, out); } Catch(IOException ex) {Logger.error ("Word Conversion Error!" ", ex); } finally {                            if(In! =NULL) {ioutils.closequietly (in); }                            if(Out! =NULL) {ioutils.closequietly (out); }                        }                        returnImagefile.getname ();                        }                });            Wordtohtmlconverter.processdocument (document); Document HTMLDocument=wordtohtmlconverter.getdocument (); Bytearrayoutputstream out=NewBytearrayoutputstream (); Domsource Domsource=NewDomsource (HTMLDocument); Streamresult Streamresult=NewStreamresult (out); 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);            Out.close (); String result=NewString (Out.tobytearray ()); FileWriter writer=NewFileWriter (parentdirectory + newName + ". html");            Writer.write (result);                                Writer.close (); } Catch(IOException | transformerexception |parserconfigurationexception ex) {Logger.error ("Word Conversion Error!" ", ex); }        return NewFile (parentdirectory + newName + ". html"); }        /*** Convert an uploaded Word document into an HTML string *@paramAttachfile *@return     */     Publicstring converttohtml (Multipartfile attachfile) {string Wordcontent= ""; //Convert a Word file to HTMLFile File =convert (attachfile); //reading HTML files        if(File! =NULL) {wordcontent=parsefile.readhtml (file); }        returnwordcontent; }    }

Java converts word to HTML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.