Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Java, how do I cause Word to open and edit a file? [duplicate]

Tags:

java

ms-word

Possible Duplicate:
Open excel document in java

I have a button in my Java application that, when clicked, should cause Word to open a particular file. This file is residing somewhere in the filesystem, like in a user's documents directory.

How can I implement something like this in Java?

like image 369
Sarah Avatar asked Jul 29 '11 10:07

Sarah


2 Answers

Here is the simple Demo App , you can modify it for button click event :

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

public class Test {
 public static void main(String[] a) {
   try {
     if (Desktop.isDesktopSupported()) {
       Desktop.getDesktop().open(new File("c:\\a.doc"));
     }
   } catch (IOException ioe) {
     ioe.printStackTrace();
  }
}

}

This would open word file with default word application . More detail here for Desktop

like image 148
Sandeep Pathak Avatar answered Oct 12 '22 01:10

Sandeep Pathak


One way is to execute the default program to open the document through the shell.

On Windows:

Process p = Runtime.getRuntime()
                .exec("rundll32 url.dll,FileProtocolHandler C:/Path/To/Word.doc");
p.waitFor();
System.out.println("Done.");

Mac:

Process p = Runtime.getRuntime().exec("open /Documents/word.doc");

From - http://www.rgagnon.com/javadetails/java-0014.html

like image 27
arunkumar Avatar answered Oct 12 '22 01:10

arunkumar