Exemple #1
0
  public void testScript2() {
    try {
      ComThread.InitSTA();
      ScriptTestSTA script = new ScriptTestSTA();
      try {
        Thread.sleep(1000);
      } catch (InterruptedException ie) {
        // should we get this?
      }

      String scriptCommand = getSampleVPScriptForEval();
      // get a thread-local Dispatch from sCon
      Dispatch sc = script.sCon.toDispatch();

      // call a method on the thread-local Dispatch obtained
      // from the DispatchProxy. If you try to make the same
      // method call on the sControl object - you will get a
      // ComException.
      Variant result = Dispatch.call(sc, "Eval", scriptCommand);
      System.out.println("eval(" + scriptCommand + ") = " + result);
      script.quit();
      System.out.println("called quit");
    } catch (ComException e) {
      e.printStackTrace();
      fail("caught exception" + e);
    } finally {
      Integer I = null;
      for (int i = 1; i < 1000000; i++) {
        I = new Integer(i);
      }
      System.out.println(I);
      ComThread.Release();
    }
  }
Exemple #2
0
  /**
   * @param path 打印路径地址,形如 \\XX\\YY.xls
   * @param copies 打印份数
   */
  public static void printExcel(String path, int copies) {
    if (path.isEmpty() || copies < 1) {
      return;
    }
    // 初始化COM线程
    ComThread.InitSTA();
    // 新建Excel对象
    ActiveXComponent xl = new ActiveXComponent("Excel.Application");
    Dispatch excel = null;
    try {
      System.out.println("Version=" + xl.getProperty("Version"));
      // 设置是否显示打开Excel
      Dispatch.put(xl, "Visible", new Variant(true));
      // 打开具体的工作簿
      Dispatch workbooks = xl.getProperty("Workbooks").toDispatch();
      excel = Dispatch.call(workbooks, "Open", path).toDispatch();

      // 设置打印属性并打印
      Dispatch.callN(
          excel,
          "PrintOut",
          new Object[] {
            Variant.VT_MISSING,
            Variant.VT_MISSING,
            new Integer(copies),
            new Boolean(false),
            PRINT_NAME,
            new Boolean(true),
            Variant.VT_MISSING,
            ""
          });

    } catch (RuntimeException e) {
      LOG.info("服务器运行时异常:{}", e.getMessage());
    } catch (Exception e) {
      LOG.info("处理打印出现异常:{}", e.getMessage());
      LOG.error("处理打印出现异常:{}.{}", e.getMessage(), e);
    } finally {
      if (excel != null) {
        // 关闭文档
        Dispatch.call(excel, "Close", new Variant(false));
      }
      if (xl != null) {
        xl.invoke("Quit", new Variant[0]);
      }
      // 始终释放资源
      ComThread.Release();
    }
  }
Exemple #3
0
 public static void htmlToWord(String html, String docFile) {
   ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
   try {
     app.setProperty("Visible", new Variant(false));
     Dispatch docs = app.getProperty("Documents").toDispatch();
     Dispatch doc =
         Dispatch.invoke(
                 docs,
                 "Open",
                 Dispatch.Method,
                 new Object[] {html, new Variant(false), new Variant(true)},
                 new int[1])
             .toDispatch();
     Dispatch.invoke(
         doc, "SaveAs", Dispatch.Method, new Object[] {docFile, new Variant(1)}, new int[1]);
     Variant f = new Variant(false);
     Dispatch.call(doc, "Close", f);
   } catch (Exception e) {
     e.printStackTrace();
   } finally {
     app.invoke("Quit", new Variant[] {});
     ComThread.Release();
   }
 }