Operating conditions: JDK + Jacob.jar + jacob.dll
1) put Jacob.dll in java_home\bin\ and java_home\jre\bin\ and C:\WINDOWS\system32\ catalogue
2) put the Jacob.jar into the project's Lib package, and load the jar package in the Java build path.
3) Run the project to compile the pass.
Note: Jacob.jar and Jacob.dll versions will match the JDK version, otherwise an exception may occur!
import Com.jacob.activex.activexcomponent;import com.jacob.com.*; Public classofficetohtml {PrivateFinalStaticofficetohtml oofficetohtml =Newofficetohtml (); Public Staticofficetohtml getinstance () {returnoofficetohtml; } Publicofficetohtml () {} PublicBoolean wordtohtml (string s, string s1) {Comthread.initsta (); Activexcomponent activexcomponent=NewActivexcomponent ("Word.Application"); String S2=s; String S3=S1; Boolean flag=false; Try{Activexcomponent.setproperty ("Visible",NewVariant (false)); Dispatch Dispatch= Activexcomponent.getproperty ("Documents"). Todispatch (); Dispatch Dispatch1= Dispatch.invoke (Dispatch,"Open",1, NewObject[] {s2,NewVariant (false),NewVariant (true) }, New int[1]). Todispatch (); Dispatch.invoke (Dispatch1,"SaveAs",1,Newobject[] {s3,NewVariant (8) },New int[1]); Variant Variant=NewVariant (false); Dispatch.call (Dispatch1,"Close", variant); Flag=true; } Catch(Exception Exception) {exception.printstacktrace (); } finally{Activexcomponent.invoke ("Quit",Newvariant[0]); Comthread.release (); Comthread.quitmainsta (); } returnFlag; } PublicBoolean ppttohtml (string s, string s1) {Comthread.initsta (); Activexcomponent activexcomponent=NewActivexcomponent ("powerpoint.application"); String S2=s; String S3=S1; Boolean flag=false; Try{Dispatch Dispatch= Activexcomponent.getproperty ("Presentations"). Todispatch (); Dispatch Dispatch1= Dispatch.call (Dispatch,"Open", S2,NewVariant (-1),NewVariant (-1),NewVariant (0) . Todispatch (); Dispatch.call (Dispatch1,"SaveAs", S3,NewVariant ( A)); Variant Variant=NewVariant (-1); Dispatch.call (Dispatch1,"Close"); Flag=true; } Catch(Exception Exception) {System. out. println ("|||"+exception.tostring ()); } finally{Activexcomponent.invoke ("Quit",Newvariant[0]); Comthread.release (); Comthread.quitmainsta (); } returnFlag; } PublicBoolean exceltohtml (string s, string s1) {Comthread.initsta (); Activexcomponent activexcomponent=NewActivexcomponent ("Excel.Application"); String S2=s; String S3=S1; Boolean flag=false; Try{Activexcomponent.setproperty ("Visible",NewVariant (false)); Dispatch Dispatch=Activexcomponent.getproperty ("Workbooks"). Todispatch (); Dispatch Dispatch1= Dispatch.invoke (Dispatch,"Open",1,Newobject[] {s2,NewVariant (false),NewVariant (true) }, New int[1]). Todispatch (); Dispatch.call (Dispatch1,"SaveAs", S3,NewVariant ( -)); Variant Variant=NewVariant (false); Dispatch.call (Dispatch1,"Close", variant); Flag=true; } Catch(Exception Exception) {System. out. println ("|||"+exception.tostring ()); } finally{Activexcomponent.invoke ("Quit",Newvariant[0]); Comthread.release (); Comthread.quitmainsta (); } returnFlag; } Public Static voidMain (String args[]) {officetohtml Otx=officetohtml.getinstance (); Boolean Flag1= Otx. Ppttohtml ("e:/test/test3.pptx","e:/test/test3.html"); if(Flag1) {System. out. println ("ppt file converted to HTML success! "); }Else{System. out. println ("ppt file converted to HTML failed! "); } }}
Office in Java (WORD/PPT/EXCEL) converted to HTML implementation