Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Excel from Java Application

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.

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.

like image 696
Manish Doshi Avatar asked Jun 24 '13 13:06

Manish Doshi


1 Answers

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

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

like image 114
Vladaimir Cetkovic Avatar answered Oct 10 '22 17:10

Vladaimir Cetkovic