从Java,当Excel US preadsheet在一个实例关闭给定的多小号preadsheets如何通知 [英] From java, how to be notified when excel spreadsheet is closed given multi spreadsheets in one instance

查看:146
本文介绍了从Java,当Excel US preadsheet在一个实例关闭给定的多小号preadsheets如何通知的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有启动Excel US preadsheet对他们的命令行,并等待它封闭如下采取下一步行动的java程序。 (顺便说一句我的Excel运行宏,以便我时自动执行我的指示是宏完成完成关闭在S preadsheet)

I have java program that starts excel spreadsheet for them command line and wait for it to be closed to take next action as below. (btw my excel runs macro so I close the spreadsheet automatically when my macro finish executing to indicate it is done)

p = Runtime.getRuntime().exec("cmd /c start /wait C:\\"+excelSheet);
p.waitFor();
System.out.print("finished "+count + " "+ excelSheet);

不过,我希望我的java程序启动Excel的2 US preadsheets,每一次一张关闭,我要采取行动。使用Excel一个实例是Excel 2010中开始能够对S preadsheets问题。因此,当将Excel的实例被关闭,这意味着能够对S preadsheets已关闭我的Java程序只检测。

However I want my java program to starts 2 excel spreadsheets and each time one sheet closes, I want to take action. The problem that excel 2010 starts both spreadsheets using one instance of Excel. Therefore my java program only detects when the instance of the Excel is closed which means both spreadsheets has to close.

我该如何解决呢?无论是Java的code,Excel的code,其他一些创新的方法?请帮忙
谢谢

How can I solve it ? Whether it is Java code, Excel code, some other innovative method? Please help Thank you

PS:我使用的Apache POI写在开始之前先争优和关闭后,从中读取

PS: I am using apache poi to write to excel before starting and read from it after closing

推荐答案

您可以定期打开文件进行写入(只是附加模式打开一个普通的FileOutputStream 就可以了 - 如果这引发IOException,则文件仍处于打开状态)

You can periodically open the file for writing (just open a normal FileOutputStream on it in append mode - if this raises an IOException then the file is still open)

public boolean isStillOpenOnWindows(File file) {
    try {
        FileOutputStream out = new FileOutputStream(file, true);
        out.close();
        // Not open anymore
        return false;
    } catch (IOException e) {
        // Still open
        return true;
    }
}

由于方法顾名思义,这是的不可以跨平台的,它仅适用于Windows,因为Windows不允许过程有开放的同时写入文件。

As the name of the method implies, this is not cross-platform, it only works on Windows because Windows doesn't allow to processes to have a file open for writing at the same time.

既然你在谈论Excel中,很可能是你的意思的Windows,但Mac版Office与这一招将不会在Mac OS上运行。

Since you're talking about Excel, it's likely that you mean Windows, but there is Office for Mac and this trick wouldn't work on Mac OS.

这篇关于从Java,当Excel US preadsheet在一个实例关闭给定的多小号preadsheets如何通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆