Using Java, how do I cause Word to open and edit a file? [duplicate]
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 di开发者_StackOverflowrectory.
How can I implement something like this in Java?
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
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
精彩评论