Java – opens an excel file using the default program
•
Java
My program successfully created and filled excel (. XLS) files Once created, I want the new file to open in the system's default program (excel in my case) How can I achieve it?
For older programs where I want to open txt files in Notepad, I use the following:
if (!Desktop.isDesktopSupported()) { System.err.println("Desktop not supported"); // use alternative (Runtime.exec) return; } Desktop desktop = Desktop.getDesktop(); if (!desktop.isSupported(Desktop.Action.EDIT)) { System.err.println("EDIT not supported"); // use alternative (Runtime.exec) return; } try { desktop.edit(new File(this.outputFilePath)); } catch (IOException ex) { ex.printStackTrace(); }
When I try to use the excel file of this code, it gives me the following error:
java.io.IOException: Failed to edit file:C:/foo.xls
Suggestions?
Solution
Try using desktop Open () instead of desktop edit():
Desktop dt = Desktop.getDesktop(); dt.open(new File(this.outputFilePath));
If desktop If open() is not available, you can use the windows file association:
Process p = Runtime.getRuntime() .exec("rundll32 url.dll,FileProtocolHandler " + this.outputFilePath);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码