我正在使用桌面应用程序java使用swing
我正在使用JFilechooser选择保存我的Excel文件的文件夹
这第一部分正常工作,文件保存得很好。但我需要在保存之后直接打开它;
我正在使用此代码找到该文件但未打开
File xlsx = new File(path + ".xls");
FileInputStream is = null;
try {
is = new FileInputStream(xlsx);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
HSSFWorkbook workbook2 = new HSSFWorkbook(is);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (xlsx.isFile() && xlsx.exists()) {
System.out.println("hurray! We've just opened a workbook");
} else {
System.out.println("Ahh! there was an error. Please make sure that the file path is correct.");
}
我发现了这个Desktop.getDesktop().open(file);
,但只要我将文件放在桌面上就可以了,我需要在保存它的地方打开它
感谢您的帮助
找到另一种解决方案
更新:
File xlsx = new File(path + ".xls");
try {
Runtime.getRuntime().exec("rundll32 SHELL32.DLL,ShellExec_RunDLL \"" + xlsx + "\"");
} catch (Exception exception) {
exception.printStackTrace();
} // path from JFileChooser();