java(word/ppt/excel)转换成HTML实现

来源:互联网 发布:php qq农场游戏源码 编辑:程序博客网 时间:2024/06/05 15:06
[java] view plaincopyprint?
  1. import com.jacob.activeX.ActiveXComponent;  
  2. import com.jacob.com.*;  
  3.   
  4. public class OfficeToXML {  
  5.   
  6.     private final static OfficeToXML oOfficeToXML = new OfficeToXML();  
  7.   
  8.     public static OfficeToXML getInstance() {  
  9.         return oOfficeToXML;  
  10.     }  
  11.   
  12.     public OfficeToXML() {  
  13.     }  
  14.   
  15.     public boolean WordtoHtml(String s, String s1) {  
  16.         ComThread.InitSTA();  
  17.         ActiveXComponent activexcomponent = new ActiveXComponent(  
  18.                 "Word.Application");  
  19.         String s2 = s;  
  20.         String s3 = s1;  
  21.         boolean flag = false;  
  22.         try {  
  23.             activexcomponent.setProperty("Visible"new Variant(false));  
  24.             Dispatch dispatch = activexcomponent.getProperty("Documents").toDispatch();  
  25.             Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open"1,  
  26.                     new Object[] { s2, new Variant(false), new Variant(true) },  
  27.                     new int[1]).toDispatch();  
  28.             Dispatch.invoke(dispatch1, "SaveAs"1new Object[] { s3,  
  29.                     new Variant(8) }, new int[1]);  
  30.             Variant variant = new Variant(false);  
  31.             Dispatch.call(dispatch1, "Close", variant);  
  32.             flag = true;  
  33.         } catch (Exception exception) {  
  34.             exception.printStackTrace();  
  35.         } finally {  
  36.             activexcomponent.invoke("Quit"new Variant[0]);  
  37.             ComThread.Release();  
  38.             ComThread.quitMainSTA();  
  39.         }  
  40.         return flag;  
  41.     }  
  42.   
  43.     public boolean PPttoHtml(String s, String s1) {  
  44.         ComThread.InitSTA();  
  45.         ActiveXComponent activexcomponent = new ActiveXComponent(  
  46.                 "PowerPoint.Application");  
  47.         String s2 = s;  
  48.         String s3 = s1;  
  49.         boolean flag = false;  
  50.         try {  
  51.             Dispatch dispatch = activexcomponent.getProperty("Presentations")  
  52.                     .toDispatch();  
  53.             Dispatch dispatch1 = Dispatch.call(dispatch, "Open", s2,  
  54.                     new Variant(-1), new Variant(-1), new Variant(0))  
  55.                     .toDispatch();  
  56.             Dispatch.call(dispatch1, "SaveAs", s3, new Variant(12));  
  57.             Variant variant = new Variant(-1);  
  58.             Dispatch.call(dispatch1, "Close");  
  59.             flag = true;  
  60.         } catch (Exception exception) {  
  61.             System.out.println("|||" + exception.toString());  
  62.         } finally {  
  63.             activexcomponent.invoke("Quit"new Variant[0]);  
  64.             ComThread.Release();  
  65.             ComThread.quitMainSTA();  
  66.         }  
  67.         return flag;  
  68.     }  
  69.   
  70.     public boolean ExceltoHtml(String s, String s1) {  
  71.          ComThread.InitSTA();  
  72.          ActiveXComponent activexcomponent = new  
  73.          ActiveXComponent("Excel.Application");  
  74.          String s2 = s;  
  75.          String s3 = s1;  
  76.          boolean flag = false;  
  77.          try  
  78.          {  
  79.          activexcomponent.setProperty("Visible"new Variant(false));  
  80.          Dispatch dispatch =  
  81.          activexcomponent.getProperty("Workbooks").toDispatch();  
  82.          Dispatch dispatch1 = Dispatch.invoke(dispatch, "Open"1new  
  83.          Object[] {  
  84.          s2, new Variant(false), new Variant(true)  
  85.          }, new int[1]).toDispatch();  
  86.          Dispatch.call(dispatch1, "SaveAs", s3, new Variant(44));  
  87.          Variant variant = new Variant(false);  
  88.          Dispatch.call(dispatch1, "Close", variant);  
  89.          flag = true;  
  90.          }  
  91.          catch(Exception exception)  
  92.          {  
  93.          System.out.println("|||" + exception.toString());  
  94.          }  
  95.          finally  
  96.          {  
  97.          activexcomponent.invoke("Quit"new Variant[0]);  
  98.          ComThread.Release();  
  99.          ComThread.quitMainSTA();  
  100.          }  
  101.          return flag;  
  102.     }  
  103.   
  104.     public static void main(String args[]) {  
  105.         OfficeToXML otx = OfficeToXML.getInstance();  
  106.         boolean flag1 = otx.PPttoHtml("e:/test/test3.pptx""e:/test/test3.html");  
  107.         if(flag1){  
  108.             System.out.println("PPT文件转换成HTML成功!");  
  109.         }else{  
  110.             System.out.println("PPT文件转换成HTML失败!");  
  111.         }  
  112.         boolean flag2 = otx.WordtoHtml("e:/test/test2.docx""e:/test/test2.html");  
  113.         if(flag2){  
  114.             System.out.println("WORD文件转换成HTML成功!");  
  115.         }else{  
  116.             System.out.println("WORD文件转换成HTML失败!");  
  117.         }  
  118.         boolean flag3 = otx.ExceltoHtml("e:/test/test1.xlsx""e:/test/test1.html");  
  119.         if(flag3){  
  120.             System.out.println("EXCEL文件转换成HTML成功!");  
  121.         }else{  
  122.             System.out.println("EXCEL文件转换成HTML失败!");  
  123.         }  
  124.     }  
  125. }  



运行条件:

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版本相匹配,否则后果自负


0 0
原创粉丝点击