Opening an Excel file using the default program
24,230
Try to use Desktop.open() instead of Desktop.edit() :
Desktop dt = Desktop.getDesktop();
dt.open(new File(this.outputFilePath));
If Desktop.open() is not available then the Windows file association can be used :
Process p =
Runtime.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler " + this.outputFilePath);
Author by
clang1234
Full stack web application developer with product experience in Ruby, PHP, and all the little bits that come along with them.
Updated on June 24, 2020Comments
-
clang1234 about 4 years
My program successfully creates and fills a Excel(.xls) file. Once created, I would like the new file to open in the system's default program (Excel in my case). How can I achieve this?
For an older program where I wanted to open a txt file in Notepad, I used 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 this code for an Excel file it gives me the following error:
java.io.IOException: Failed to edit file:C:/foo.xls
Suggestions?
-
StarCrafter over 8 yearsYes. Jexcel API prints colors more realistic than JRXlsExporter library
-
RealHowTo almost 8 years@suman, maybe
xdg-open
to open the default application for a given file type, see manpages.ubuntu.com/manpages/trusty/en/man1/xdg-open.1.html