我的程序成功創建並填充了Excel(.xls)文件。一旦創建,我想在系統的默認程序中打開新文件(在我的情況下爲Excel)。我怎樣才能做到這一點?使用默認程序打開Excel文件
對於較舊的程序,我想用記事本打開一個txt文件,我用了以下內容:
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();
}
當我嘗試使用此代碼爲一個Excel文件,它給了我下面的錯誤:
java.io.IOException: Failed to edit file:C:/foo.xls
建議?
您可能會在這裏找到類似的答案: –
jayaneetha
2016-04-11 08:51:18