Java – 从Windows中的代码执行Excel.exe

我已经开发了一个在某个时候需要打开一个已有的excel文件的软件,这个文件是由用户select的。

这里的方法是:

private void openChoiceActionPerformed(java.awt.event.ActionEvent evt) { // TODO add your handling code here: FileDialog fd = new FileDialog(this, "Open file", FileDialog.LOAD); String osName = System.getProperty("os.name"); //osName = osName.toLowerCase(); fd.setLocation(600,200); fd.setFile("*.xlsx"); fd.setVisible(true); String dir = fd.getDirectory(); String fileName = fd.getFile(); String path = dir + fileName; File file = new File(path); if(file != null){ try { FileInputStream input = new FileInputStream(file); } catch (FileNotFoundException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); } } if(osName.equals("Linux")){ try { Process p = Runtime.getRuntime().exec("libreoffice --calc "+ path); } catch (IOException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(rootPane, "Errore nell'apertura del file: " +ex); } }else{ try { Process p = Runtime.getRuntime().exec("C:\\Program Files (x86)\\Microsoft Office\\Office15\\EXCEL.EXE "+ path); } catch (IOException ex) { Logger.getLogger(Gui.class.getName()).log(Level.SEVERE, null, ex); JOptionPane.showMessageDialog(rootPane, "Errore nell'apertura del file: " +ex); } } } 

该软件需要在Linux和Windows上工作,所以我已经在OS的function上采取了可执行文件的path

在Linux上,一切正常!

在Windows上,我得到一个错误:

无法运行程序“C:\ Program Files(x86)\ Microsoft Office \ Office15 \ EXCEL.EXE”创build进程错误= 5 Acess denied

我只需要运行excel,但我不知道如何解决这个问题,因为C:\Program Files (x86)不适用于应用程序