在 Office 2010 中使用 JACOB 打开使用 POI 生成的 Excel 电子表格时出现问题

发布于 2024-10-06 10:04:11 字数 2855 浏览 7 评论 0原文

在工作中,我们有一个非常古老但稳定的小程序,它需要两个 Excel 电子表格(一个用作模板,另一个用作数据源)。模板由用户在 Excel 中创建,但数据源是在 Web 应用程序中使用 POI 创建的。在小程序中,两个电子表格均通过 Java/COM 桥 (JACOB) 使用以下方法打开:

/**
 * Opens an excel workbook
 */
public Object openExcelWorkBook(ActiveXComponent xlActx, MailMergeArgs args, String dataSourceDocName, Object xlObj,
                                boolean isTemplate) {
  final String functionName = "openExcelWorkBook";
  Object activeWorkBookOj;
  String workbook;
  // get the workbooks object
  Object wrkBooksObj = Dispatch.get(xlActx, ExcelConstants.APPLICATION_WORKBOOKS).toDispatch();

  if ((!args.isTemplateExistsOnHost()) && (isTemplate)) {
    debug("Skipping opening template...", functionName);
  }
  // it is on host
  else {
    if (isTemplate) {
      workbook = getTemplateDocLoc(args);
    } else {
      if (!this.standaloneApp) {
        workbook = TMP_DIR + dataSourceDocName;
      } else {
        workbook = dataSourceDocName;
      }
    }

    debug("Opening workbook..." + workbook, functionName);
    Dispatch.call(wrkBooksObj, ExcelConstants.WORKBOOKS_OLE_OPEN, workbook).toDispatch();

  }

  debug("Locating workbook...", functionName);
  activeWorkBookOj = Dispatch.get(xlActx, ExcelConstants.APPLICATION_ACTIVEWORKBOOK).toDispatch();

  debug("End", functionName);
  return activeWorkBookOj;
}

从小程序日志和 hs_err_pid 文件中,我知道 JACOB 可以正常打开模板表,但数据表在 Dispatch 上失败。称呼。

来自 hs_err_pid 文件:

> Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
> j  com.jacob.com.Dispatch.invokev(Ljava/lang/Object;Ljava/lang/String;III[Lcom/jacob/com/Variant;[I)Lcom/jacob/com/Variant;+0
> j  com.jacob.com.Dispatch.invokev(Ljava/lang/Object;II[Lcom/jacob/com/Variant;[I)Lcom/jacob/com/Variant;+27
> j  com.jacob.com.Dispatch.callN(Ljava/lang/Object;I[Ljava/lang/Object;)Lcom/jacob/com/Variant;+11
> j  com.jacob.com.Dispatch.call(Ljava/lang/Object;ILjava/lang/Object;)Lcom/jacob/com/Variant;+10
> j  com.perceptive.ctms.applets.ImpactApplet.openExcelWorkBook(Lcom/jacob/activeX/ActiveXComponent;Lcom/perceptive/ctms/applets/MailMergeArgs;Ljava/lang/String;Ljava/lang/Object;Z)Ljava/lang/Object;+122
> j  com.perceptive.ctms.applets.ImpactApplet.doExcelMailMerge(Lcom/perceptive/ctms/applets/MailMergeArgs;Ljava/lang/String;Ljava/lang/String;I)V+234
> j  com.perceptive.ctms.applets.ImpactApplet.runOrams(Lcom/perceptive/ctms/applets/MailMergeArgs;)V+320
> j  com.perceptive.ctms.applets.ImpactApplet.start()V+94
> j  sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run()V+1159
> j  java.lang.Thread.run()V+11
> v  ~StubRoutines::call_stub

自从在 Office 2010 上进行测试以来,我们才遇到此问题。

我们使用 POI 2.5.1 和 JACOB 1.9,它们在过去几年中都已继续发展,我打算尝试升级它们,但我想知道如果项目和 Office 2010 存在已知问题。虽然 POI 3.x 支持 OOXML,但我们的代码需要保持与早期 Office 版本的向后兼容,因此我们更愿意坚持使用 HSSF 生成。

At work we have a very old, but stable, applet that takes two excel spreadsheets (one acts as a template and the other as a data source). The template is created by the user in Excel, but the data source is created using POI in a web application. Within the applet, both spreadsheets are opened via the Java/COM Bridge (JACOB), using the following method:

/**
 * Opens an excel workbook
 */
public Object openExcelWorkBook(ActiveXComponent xlActx, MailMergeArgs args, String dataSourceDocName, Object xlObj,
                                boolean isTemplate) {
  final String functionName = "openExcelWorkBook";
  Object activeWorkBookOj;
  String workbook;
  // get the workbooks object
  Object wrkBooksObj = Dispatch.get(xlActx, ExcelConstants.APPLICATION_WORKBOOKS).toDispatch();

  if ((!args.isTemplateExistsOnHost()) && (isTemplate)) {
    debug("Skipping opening template...", functionName);
  }
  // it is on host
  else {
    if (isTemplate) {
      workbook = getTemplateDocLoc(args);
    } else {
      if (!this.standaloneApp) {
        workbook = TMP_DIR + dataSourceDocName;
      } else {
        workbook = dataSourceDocName;
      }
    }

    debug("Opening workbook..." + workbook, functionName);
    Dispatch.call(wrkBooksObj, ExcelConstants.WORKBOOKS_OLE_OPEN, workbook).toDispatch();

  }

  debug("Locating workbook...", functionName);
  activeWorkBookOj = Dispatch.get(xlActx, ExcelConstants.APPLICATION_ACTIVEWORKBOOK).toDispatch();

  debug("End", functionName);
  return activeWorkBookOj;
}

From the applet log and an hs_err_pid file I know the template sheet is opened fine by JACOB, but the data sheet fails on the Dispatch.call.

From the hs_err_pid file:

> Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
> j  com.jacob.com.Dispatch.invokev(Ljava/lang/Object;Ljava/lang/String;III[Lcom/jacob/com/Variant;[I)Lcom/jacob/com/Variant;+0
> j  com.jacob.com.Dispatch.invokev(Ljava/lang/Object;II[Lcom/jacob/com/Variant;[I)Lcom/jacob/com/Variant;+27
> j  com.jacob.com.Dispatch.callN(Ljava/lang/Object;I[Ljava/lang/Object;)Lcom/jacob/com/Variant;+11
> j  com.jacob.com.Dispatch.call(Ljava/lang/Object;ILjava/lang/Object;)Lcom/jacob/com/Variant;+10
> j  com.perceptive.ctms.applets.ImpactApplet.openExcelWorkBook(Lcom/jacob/activeX/ActiveXComponent;Lcom/perceptive/ctms/applets/MailMergeArgs;Ljava/lang/String;Ljava/lang/Object;Z)Ljava/lang/Object;+122
> j  com.perceptive.ctms.applets.ImpactApplet.doExcelMailMerge(Lcom/perceptive/ctms/applets/MailMergeArgs;Ljava/lang/String;Ljava/lang/String;I)V+234
> j  com.perceptive.ctms.applets.ImpactApplet.runOrams(Lcom/perceptive/ctms/applets/MailMergeArgs;)V+320
> j  com.perceptive.ctms.applets.ImpactApplet.start()V+94
> j  sun.plugin2.applet.Plugin2Manager$AppletExecutionRunnable.run()V+1159
> j  java.lang.Thread.run()V+11
> v  ~StubRoutines::call_stub

We have only experienced this problem since testing on Office 2010.

We use POI 2.5.1 and JACOB 1.9 which have both moved on in the last few years, I intend to try upgrading them, but I would like to know if there are known issues with either project and Office 2010. Although POI 3.x supports OOXML our code needs to remain backwards compatible with earlier Office versions, so we would prefer to stick with generating using HSSF.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文