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

时间:2022-06-17 20:30:16
try {
    Runtime.getRuntime().exec("excel C:\\file.xls");
} catch (IOException ex) {
    System.out.println(ex);
}

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?

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

4 个解决方案

#1


10  

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

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

public void open(File file) throws IOException

which is documented as what you want to do:

记录为您想要做的事情:

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.

启动关联的应用程序以打开文件。如果指定的文件是目录,则启动当前平台的文件管理器以将其打开。

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

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

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

#2


1  

I use Runtime rt = Runtime.getRuntime().exec("cmd.exe /C start " + *filename* it works for me on Windows platforms

我使用Runtime rt = Runtime.getRuntime()。exec(“cmd.exe / C start”+ * filename *它适用于Windows平台

#3


0  

Call the Windows "start.exe" command instead of Excel directly. Start.exe appears to search paths, etc. However, it still may not find it if it's not in the path.

直接调用Windows“start.exe”命令而不是Excel。 Start.exe似乎搜索路径等。但是,如果它不在路径中,它仍然可能找不到它。

#4


0  

You might try using "cmd" instead of "excel", and then pass in an array of params.

您可以尝试使用“cmd”而不是“excel”,然后传入一组参数。

For easier debugging, you might also try using ProcessBuilder instead. In my experience it's much nicer to work with: http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html

为了便于调试,您也可以尝试使用ProcessBuilder。根据我的经验,使用它更好:http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html

#1


10  

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

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

public void open(File file) throws IOException

which is documented as what you want to do:

记录为您想要做的事情:

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.

启动关联的应用程序以打开文件。如果指定的文件是目录,则启动当前平台的文件管理器以将其打开。

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

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

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

#2


1  

I use Runtime rt = Runtime.getRuntime().exec("cmd.exe /C start " + *filename* it works for me on Windows platforms

我使用Runtime rt = Runtime.getRuntime()。exec(“cmd.exe / C start”+ * filename *它适用于Windows平台

#3


0  

Call the Windows "start.exe" command instead of Excel directly. Start.exe appears to search paths, etc. However, it still may not find it if it's not in the path.

直接调用Windows“start.exe”命令而不是Excel。 Start.exe似乎搜索路径等。但是,如果它不在路径中,它仍然可能找不到它。

#4


0  

You might try using "cmd" instead of "excel", and then pass in an array of params.

您可以尝试使用“cmd”而不是“excel”,然后传入一组参数。

For easier debugging, you might also try using ProcessBuilder instead. In my experience it's much nicer to work with: http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html

为了便于调试,您也可以尝试使用ProcessBuilder。根据我的经验,使用它更好:http://download.oracle.com/javase/6/docs/api/java/lang/ProcessBuilder.html