Java - 使用runtime.getRuntime()运行Excel [英] Java - Run Excel using runtime.getRuntime().exec

查看:289
本文介绍了Java - 使用runtime.getRuntime()运行Excel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

try {
    Runtime.getRuntime().exec("excel C:\\file.xls");
} catch (IOException ex) {
    System.out.println(ex);
}

无效。
我必须把excel.exe的完整路径工作。
如何使其通用(对于任何Excel文件夹/版本)?
当我从Windows运行(开始 - >运行)
的操作系统运行相同的行时,它可以工作。在Java中有一个代码来模拟Windows的运行命令?

Doesn't work. I have to put the full path of excel.exe in order to work. How can I make it generic (For any Excel Folders/Versions)? When I run the same line from OS with Windows Run (Start --> Run) it works. Is there a code in Java to simulate Windows' Run command?

推荐答案

为什么不尝试使用桌面 class(api doc 这里)在JDK6中引入了方法

Why don't you try with the Desktop class (api doc here) introduced in JDK6 that has the method

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 扩展名由操作系统映射到Excel 。然后,您可以使用

Of course this assumes that .xls extension is mapped by OS to Excel. Then you can go with

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

这篇关于Java - 使用runtime.getRuntime()运行Excel的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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