java – 使用默认程序打开Excel文件
发布时间:2020-12-14 05:25:16 所属栏目:Java 来源:网络整理
导读:我的程序成功创建并填充Excel(.xls)文件.一旦创建,我希望新文件在系统的默认程序(我的情况下为Excel)中打开.我该如何实现? 对于我想在记事本中打开txt文件的较旧的程序,我使用以下内容: if (!Desktop.isDesktopSupported()) { System.err.println("Desktop
我的程序成功创建并填充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(); } 当我尝试使用这个代码的Excel文件,它给我以下错误: java.io.IOException: Failed to edit file:C:/foo.xls 建议? 解决方法
尝试使用Desktop.open()而不是Desktop.edit():
Desktop dt = Desktop.getDesktop(); dt.open(new File(this.outputFilePath)); 如果Desktop.open()不可用,则可以使用Windows文件关联: Process p = Runtime.getRuntime() .exec("rundll32 url.dll,FileProtocolHandler " + this.outputFilePath); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |