从 Java 应用程序打开 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17276688/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:33:37  来源:igfitidea点击:

Open Excel from Java Application

javaexcelruntime

提问by Manish Doshi

If I am tring to open notepad from Java Application then it will open Notepad.But If I will try to open Excel then it is giving me exception.

如果我想从 Java 应用程序打开记事本,那么它会打开记事本。但是如果我尝试打开 Excel,那么它会给我一个例外。

try
    {
        Runtime.getRuntime().exec("excel");
    }
    catch (IOException e) 
    {
      e.printStackTrace();
    }

Following is Exception :

以下是例外:

java.io.IOException: Cannot run program "excel": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at java.lang.Runtime.exec(Unknown Source)
    at OpenNotepad.main(OpenNotepad.java:18)
Caused by: java.io.IOException: CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessImpl.create(Native Method)
    at java.lang.ProcessImpl.<init>(Unknown Source)
    at java.lang.ProcessImpl.start(Unknown Source)
    ... 5 more

When I do start->Run->Excel then it will open excel.

当我开始-> 运行-> Excel 时,它会打开 excel。

回答by Vladaimir Cetkovic

If you are trying to use Java to open an Excel file, rather than just opening Excel, I suggest you use the Desktop API class: http://docs.oracle.com/javase/6/docs/api/java/awt/Desktop.html

如果您尝试使用 Java 打开 Excel 文件,而不仅仅是打开 Excel,我建议您使用桌面 API 类:http: //docs.oracle.com/javase/6/docs/api/java/awt/桌面.html

This class will use the default OS file handling mechanism, so it will use say MS Excel in Windows, and Open Office in Linux.

此类将使用默认的 OS 文件处理机制,因此它将在 Windows 中使用 MS Excel,在 Linux 中使用 Open Office。

回答by Vimlesh

Runtime.getRuntime().exec("some command");does the same as "some command" will do in cmd. So the solution for opening Excel you should try this.

Runtime.getRuntime().exec("some command");与“某些命令”在 cmd 中的作用相同。所以打开Excel的解决方案你应该试试这个。

try {
    Runtime.getRuntime().exec("cmd /c start excel.exe");
} catch (IOException e) {
    e.printStackTrace();
}

回答by Kumar

Add microsoft office excel executable path for eg: If Directory C:\Program Files (x86)\Microsoft Office\Office14 has EXCEL.EXE then add C:\Program Files (x86)\Microsoft Office\Office14 to your classpath. How to add to classpath Click here. Once you are done, restart your IDE, your program should work.

添加 microsoft office excel 可执行路径,例如:如果目录 C:\Program Files (x86)\Microsoft Office\Office14 有 EXCEL.EXE,则将 C:\Program Files (x86)\Microsoft Office\Office14 添加到您的类路径。如何添加到类路径 单击此处。完成后,重新启动 IDE,您的程序应该可以运行了。

回答by shreyansh jogi

in the exec method give fuuly qualified path where your excel is installed and then try and RunTime is good but better to use ProcessBuilder

在 exec 方法中给出安装 excel 的完全限定路径,然后尝试 RunTime 是好的,但最好使用 ProcessBuilder

回答by Bhavik Shah

Runtime.getRuntime().exec()is the same thing as doing something from the command line.

Runtime.getRuntime().exec()与从命令行执行某些操作相同。

In windows OS there is an environment variable set to C:\Windows\System32all the exe of commands are in this path.

在 Windows 操作系统中,有一个环境变量设置为C:\Windows\System32所有命令的 exe 都在此路径中。

when I do following in cmd I get this.

当我在 cmd 中执行以下操作时,我得到了这个。

C:\Users\bhaviksh>echo %PATH%
D:\oracle\product.2.0\client_1\bin;C:\Windows\system32;C:\Windows;C:\Windows\
System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\MySQL
\MySQL Server 5.0\bin;C:\Program Files\cvsnt;C:\Program Files\TortoiseSVN\bin;C:
\maven\bin

Solution :use fully qualified name of exe file

解决方案:使用exe文件的全限定名