cla*_*234 11 java excel jexcelapi
我的程序成功创建并填充了Excel(.xls)文件.一旦创建,我希望在系统的默认程序(在我的情况下为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();
}
Run Code Online (Sandbox Code Playgroud)
当我尝试将此代码用于Excel文件时,它会给我以下错误:
java.io.IOException: Failed to edit file:C:/foo.xls
Run Code Online (Sandbox Code Playgroud)
建议?
Rea*_*wTo 30
尝试使用Desktop.open()而不是Desktop.edit():
Desktop dt = Desktop.getDesktop();
dt.open(new File(this.outputFilePath));
Run Code Online (Sandbox Code Playgroud)
如果Desktop.open()不可用,则可以使用Windows文件关联:
Process p =
Runtime.getRuntime()
.exec("rundll32 url.dll,FileProtocolHandler " + this.outputFilePath);
Run Code Online (Sandbox Code Playgroud)