在java中保存和打开excel文件

在java中保存和打开excel文件,java,Java,我想将“Jtable”上的数据导出到excel文件,导出成功后,将显示一个按摩对话框,用户可以选择是否打开保存的文件。我已经知道如何保存文件了。但我不知道如何打开我刚刚保存的文件。为了做到这一点,我认为这两条线需要固定 Desktop dt = Desktop.getDesktop(); dt.open(new File(this.file)); 下面是到目前为止我的代码 private void ExportbuttonActionPerformed(java.awt.event.Actio

我想将“Jtable”上的数据导出到excel文件,导出成功后,将显示一个按摩对话框,用户可以选择是否打开保存的文件。我已经知道如何保存文件了。但我不知道如何打开我刚刚保存的文件。为了做到这一点,我认为这两条线需要固定

Desktop dt = Desktop.getDesktop();
dt.open(new File(this.file));
下面是到目前为止我的代码

private void ExportbuttonActionPerformed(java.awt.event.ActionEvent evt) {                                             
    try{
        JFileChooser fc = new JFileChooser();
        int option = fc.showSaveDialog(SearchGUI.this);
        if(option == JFileChooser.APPROVE_OPTION){
            try{
                String filename = fc.getSelectedFile().getName(); 
                String path = fc.getSelectedFile().getParentFile().getPath();

                int len = filename.length();
                String ext = "";
                String file = "";

                if(len > 4){
        ext = filename.substring(len-4, len);
                }

                if(ext.equals(".xls")){
        file = path + "\\" + filename; 
                }else{
        file = path + "\\" + filename + ".xls"; 
                }
                toExcel(PhoneBook, new File(file));
                int answer = JOptionPane.showConfirmDialog(null, "Would you like to open the exported file?", "Successfully exported!", option);
                if (answer == JOptionPane.YES_OPTION) {
                    try {
                        Desktop dt = Desktop.getDesktop();
                        dt.open(new File(this.file));

                    } catch (Exception e ){
                                    JOptionPane.showMessageDialog(null, e);
                    }
                }

                }catch(Exception e){
                                JOptionPane.showMessageDialog(null, e);
                }
        }
    }
    catch(Exception e){
    JOptionPane.showMessageDialog(null, e);
    }
}

请帮忙

捕捉到了应该发生的错误

Desktop dt = Desktop.getDesktop();
dt.open(new File(file));
而不是

Desktop dt = Desktop.getDesktop();
dt.open(new File(this.file)); 

使用ApachePOI操作excel文件!