使用jacob对word.excel文件进行完美解析(缺点是不支持linux)

来源:互联网 发布:淘宝摄影服务市场 编辑:程序博客网 时间:2024/06/07 02:06

  /**
  * 将Word转换为HTML
  *
  * @param docfile
  * @param htmlfile
  */
 public static void change(String docfile, String htmlfile) {
  logger.info("进入" + TAG + "change()");
  System.out.println("docfile=" + docfile + "  htmlfile=" + htmlfile);
  logger.info("docfile=" + docfile + "  htmlfile=" + htmlfile);

  ActiveXComponent app = null; // 启动word
  try {
   app = new ActiveXComponent("Word.Application"); // 启动word
   app.setProperty("Visible", new Variant(false));
   // 设置word不可见
   Dispatch docs = app.getProperty("Documents").toDispatch();

   Dispatch doc = Dispatch.invoke(
     docs,
     "Open",
     Dispatch.Method,
     new Object[] { docfile, new Variant(false),
       new Variant(true) }, new int[1]).toDispatch();
   // 打开word文件
   Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {
     htmlfile, new Variant(8) }, new int[1]);
   // 作为html格式保存到临时文件
   Variant f = new Variant(false);
   Dispatch.call(doc, "Close", f);
  } catch (Exception e) {
   logger.info("进入" + TAG + "change()" + e);
   // e.printStackTrace();
  } finally {
   app.invoke("Quit", new Variant[] {});
  }
 }

 public static void main(String[] args) {
  change(
    "http:/10.11.0.111:8914/ElectronicMagazine/TemporaryFile/090709163210_1.doc",
    "E://YQ/0907091615261.html");
 }

 

需要将jacob.dll文件放到jdk的bin目录下,不然会报错

 

 

 

  /**
  * 将Excel转换为HTML
  *
  * @param docfile
  * @param htmlfile
  */

ActiveXComponent app =new ActiveXComponent("Excel.Application");// 启动excel
try {
    app.setProperty("Visible",new Variant(false));
    Dispatch excels = app.getProperty("Workbooks").toDispatch();
    Dispatch excel = Dispatch.invoke(excels,"Open", Dispatch.Method,
        newObject[] {"d:/aaa.xls",new Variant(false),new Variant(true)},new int[1]).toDispatch();
    Dispatch
        .invoke(excel,"SaveAs", Dispatch.Method,new Object[] {"d:/aaa.html",new Variant(44)},new int[1]);
    Variant f =new Variant(false);
    Dispatch.call(excel,"Close", f);
} catch(Exception e) {
    e.printStackTrace();
} finally{
    app.invoke("Quit",new Variant[] {});
}

 

 

/相关的jacob的dll 文件以及jar包 在本博客的资源里面有。可以下载

 

 

 

 

 

 

原创粉丝点击