java使用jacob将word,excel,ppt转成html

来源:互联网 发布:淘宝店标图片 编辑:程序博客网 时间:2024/05/18 02:04

用到的jar包及插件:

        jacob-1.18( jacob.jar)

       jacob-1.18( jacob-1.18-x64.dll)(32位)

       jacob-1.18( jacob-1.18-x86.dll)(64位)

1.dll文件根据电脑系统版本和jdk版本(32或者64位)来选择。

2.使用main方法时将dll文件放到C盘Windows\System32目录下,项目调用此方法时将dll文件放到jdk的jre\bin目录下。

3.需要注意一点如果运行还是报错的话,把x64.dll(32位)放到Windows\System32目录下,把x86.dll(32位)放到

Windows\SysWOW64目录下。这样肯定可行。

4.然后把jar包导入到项目工程中,注意对应版本号。

注:此方法来源与网上,然后我给整合了一下。网上的ppt转html我怎么试都不成功,转换失败全是乱码,后来我只能

把ppt转成PDF,然后再将PDF转成html。虽然麻烦但是也能实现最终的目的,谁有成功的方法欢迎分享,谢谢。

下面直接贴java代码:

public class OfficeToXML {         private final static OfficeToXML oOfficeToXML = new OfficeToXML();  public static OfficeToXML getInstance() {          return oOfficeToXML;      } public OfficeToXML() {      }  //Word转html方法   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));              activexcomponent.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏              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;      }      //excel转html方法    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));           activexcomponent.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏           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;      }      // PPT转PDF ,然后PDF再转html      /*转PDF格式值*/       private static final int ppFormatPDF = 32;       public boolean PPT2pdf(String filename, String pdfFilename) {     ComThread.InitSTA();      long start = System.currentTimeMillis();           ActiveXComponent app = null;           Dispatch ppt = null;       boolean flag = false;       try {     app = new ActiveXComponent("PowerPoint.Application");// 创建一个PPT对象                app.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏               Dispatch ppts = app.getProperty("Presentations").toDispatch();// 获取文挡属性                System.out.println("打开文档 >>> " + filename);               // 调用Documents对象中Open方法打开文档,并返回打开的文档对象Document               ppt = Dispatch.call(ppts, "Open", filename,                        true,// ReadOnly                       true,// Untitled指定文件是否有标题                       false// WithWindow指定文件是否可见                       ).toDispatch();                              System.out.println("转换文档 [" + filename + "] >>> [" + pdfFilename + "]");               Dispatch.call(ppt, "SaveAs", pdfFilename, ppFormatPDF);                  long end = System.currentTimeMillis();                  System.out.println("用时:" + (end - start) + "ms.");                            flag = true;  } catch (Exception e) { e.printStackTrace();           System.out.println("========Error:文档转换失败:" + e.getMessage());  }finally {              Dispatch.call(ppt, "Close");              System.out.println("关闭文档");              if (app != null)                  app.invoke("Quit", new Variant[] {});          }          ComThread.Release();          ComThread.quitMainSTA();                return flag;      }     public static void main(String args[]) {          OfficeToXML otx = OfficeToXML.getInstance();               boolean flag1 =otx.PPT2pdf("E:\\office\\后台架构.pptx", "E:\\office\\后台架构.pdf");        if(flag1){              System.out.println("PPT文件转换成PDF成功!");          }else{              System.out.println("PPT文件转换成PDF失败!");          }             boolean flag2 = otx.WordtoHtml("E:\\office\\产品需求文档.docx", "E:\\office\\产品需求文档.html");          if(flag2){              System.out.println("WORD文件转换成HTML成功!");          }else{              System.out.println("WORD文件转换成HTML失败!");          }        boolean flag3 = otx.ExceltoHtml("E:\\office\\bug.xlsx", "E:\\office\\bug.html");          if(flag3){              System.out.println("EXCEL文件转换成HTML成功!");          }else{              System.out.println("EXCEL文件转换成HTML失败!");          }     }}




      

原创粉丝点击