运行条件:
1.JDK1.6
2.jacob.jar和jacob.dll
1) 把jacob.dll在 ..\Java\jdk1.6.0_10\bin、..\Java\jdk1.6.0_10\jre\bin、C:\WINDOWS\system32 目录下各放一份
2) 把jacob.jar放入 项目的lib包下,并且在“java构建路径”中也要加载此jar包。
3) 运行项目即可编译通过.
注:jacob.jar以及jacob.dll版本一定要和jdk版本相匹配,否则后果自负!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
import com.jacob.activeX.ActiveXComponent; import com.jacob.com.*; public class OfficeToXML { private final static OfficeToXML oOfficeToXML = new OfficeToXML(); public static OfficeToXML getInstance() { return oOfficeToXML; } public OfficeToXML() { } public boolean WordtoHtml(String s, String s1) { ComThread.InitSTA(); ActiveXComponent activexcomponent = new ActiveXComponent( "Word.Application"); String s2 = s; String s3 = s1; boolean flag = false; try { activexcomponent.setProperty("Visible", new Variant(false)); Dispatch dispatch = activexcomponent.getProperty("Documents").toDispatch(); Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1, new Object[] { s2, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); Dispatch.invoke(dispatch1, "SaveAs", 1, new Object[] { s3, new Variant(8) }, new int[1]); Variant variant = new Variant(false); Dispatch.call(dispatch1, "Close", variant); flag = true; } catch (Exception exception) { exception.printStackTrace(); } finally { activexcomponent.invoke("Quit", new Variant[0]); ComThread.Release(); ComThread.quitMainSTA(); } return flag; } public boolean PPttoHtml(String s, String s1) { ComThread.InitSTA(); ActiveXComponent activexcomponent = new ActiveXComponent( "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, new Variant(-1), new Variant(-1), new Variant(0)) .toDispatch(); Dispatch.call(dispatch1, "SaveAs", s3, new Variant(12)); Variant variant = new Variant(-1); Dispatch.call(dispatch1, "Close"); flag = true; } catch (Exception exception) { System.out.println("|||" + exception.toString()); } finally { activexcomponent.invoke("Quit", new Variant[0]); ComThread.Release(); ComThread.quitMainSTA(); } return flag; } public boolean ExceltoHtml(String s, String s1) { ComThread.InitSTA(); ActiveXComponent activexcomponent = new ActiveXComponent("Excel.Application"); String s2 = s; String s3 = s1; boolean flag = false; try { activexcomponent.setProperty("Visible", new Variant(false)); Dispatch dispatch = activexcomponent.getProperty("Workbooks").toDispatch(); Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open", 1, new Object[] { s2, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); Dispatch.call(dispatch1, "SaveAs", s3, new Variant(44)); Variant variant = new Variant(false); Dispatch.call(dispatch1, "Close", variant); flag = true; } catch(Exception exception) { System.out.println("|||" + exception.toString()); } finally { activexcomponent.invoke("Quit", new Variant[0]); ComThread.Release(); ComThread.quitMainSTA(); } return flag; } public static void main(String args[]) { OfficeToXML otx = OfficeToXML.getInstance(); boolean flag1 = otx.PPttoHtml("e:/test/test3.pptx", "e:/test/test3.html"); if(flag1){ System.out.println("PPT文件转换成HTML成功!"); }else{ System.out.println("PPT文件转换成HTML失败!"); } boolean flag2 = otx.WordtoHtml("e:/test/test2.docx", "e:/test/test2.html"); if(flag2){ System.out.println("WORD文件转换成HTML成功!"); }else{ System.out.println("WORD文件转换成HTML失败!"); } boolean flag3 = otx.ExceltoHtml("e:/test/test1.xlsx", "e:/test/test1.html"); if(flag3){ System.out.println("EXCEL文件转换成HTML成功!"); }else{ System.out.println("EXCEL文件转换成HTML失败!"); } } } |