加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

Java – 使用runtime.getRuntime().exec运行Excel

发布时间:2020-12-15 04:53:10 所属栏目:Java 来源:网络整理
导读:try { Runtime.getRuntime().exec("excel C:file.xls");} catch (IOException ex) { System.out.println(ex);} 不行. 我必须放入excel.exe的完整路径才能工作. 如何使其通用(对于任何Excel文件夹/版本)? 当我使用Windows运行从操作系统运行相同的行时(开
try {
    Runtime.getRuntime().exec("excel C:file.xls");
} catch (IOException ex) {
    System.out.println(ex);
}

不行.
我必须放入excel.exe的完整路径才能工作.
如何使其通用(对于任何Excel文件夹/版本)?
当我使用Windows运行从操作系统运行相同的行时(开始 – >运行)
有用. Java中是否有代码来模拟Windows的Run命令?

解决方法

为什么不尝试使用JDK6中引入的具有该方法的Desktop类(api doc here)

public void open(File file) throws IOException

记录为您想要做的事情:

Launches the associated application to open the file.
If the specified file is a directory,the file manager of the current platform is launched to open it.

当然,这假设.xls扩展名由OS映射到Excel.然后你可以去

Desktop.getDesktop().open(new File("c:file.xls"));

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读