编程语言
首页 > 编程语言> > Java – 使用runtime.getRuntime().exec运行Excel

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

作者:互联网

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"));

标签:java,excel,runtime,exec,runtime-exec
来源: https://codeday.me/bug/20190715/1472126.html