如何在java中保存扩展名为.xls的excel文件?

时间:2015-04-14 07:54:04

标签: java excel

我必须保存一个扩展名为.xls的excel文件。我想在我使用文件选择器设计的保存文件对话框中选择文件类型时默认采用扩展名。我已经尝试了这么多代码但只有当我将文件名称为filename.xls

时,它才会以.xls格式保存文件
public void FileSave() throws IOException{             

   JFileChooser chooser=new JFileChooser(".");       
   FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel files","xls","excel");       
   chooser.addChoosableFileFilter(filter);      
   chooser.setFileFilter(filter);
   chooser.setFileSelectionMode(chooser.FILES_AND_DIRECTORIES);
   chooser.setDialogTitle("Save File");
   chooser.setCurrentDirectory(new File(System.getProperties().getProperty("user.home")));
   chooser.setFileFilter(new javax.swing.filechooser.FileFilter(){
        public boolean accept(final File f){
            return f.isDirectory()|| file.getAbsolutePath().endsWith(".xls");
        }

        public String getDescription(){
            return "Excel files (*.xls)";
        }
  });

  int returnVal1=chooser.showSaveDialog(this);
  if (returnVal1 == JFileChooser.APPROVE_OPTION){
        file1 = chooser.getSelectedFile();

        if(!file1.exists()){
            FileOutputStream fileOut =  new FileOutputStream(file1);
            hwb.write(fileOut);
            fileOut.close();
            System.out.println("\n Your Excel file has been generated!");
            JOptionPane.showMessageDialog(this,"File Created.");
        }
        else if(file1.exists()){
            int res=JOptionPane.showConfirmDialog(this,"File already exists.Do you wish to overwrite?");
            if(res == JOptionPane.YES_OPTION){
                FileOutputStream fileOut =  new FileOutputStream(file1);
                hwb.write(fileOut);
                fileOut.close();
                System.out.println("\n Your Excel file has been generated!");
                JOptionPane.showMessageDialog(this,"File Created.");
            }else if(res == JOptionPane.NO_OPTION){
                int returnVal2=chooser.showSaveDialog(this);
                if (returnVal2 == JFileChooser.APPROVE_OPTION){

                    File file2 = chooser.getSelectedFile();
                    if(!file2.exists()){

                        FileOutputStream fileOut =  new FileOutputStream(file2);
                        hwb.write(fileOut);
                        fileOut.close();
                        System.out.println("\n Your Excel file has been generated!");
                        JOptionPane.showMessageDialog(this,"File Created.");
                    }

                }
            }else if (res == JOptionPane.CANCEL_OPTION){
                JOptionPane.showMessageDialog(this, "User cancelled operation.");
            } 
        }
    }    
}  

2 个答案:

答案 0 :(得分:0)

您需要使用此选项将文件名设置为* .xls:

chooser.setSelectedFile("*.xls");

对于文件类型下拉列表,请设置以下选项:

JFileChooser chooser = new JFileChooser();
FileNameExtensionFilter filter = new FileNameExtensionFilter("Excel files","xls","excel");       
chooser.addChoosableFileFilter(filter);     
chooser.setFileFilter(filter);
chooser.setAcceptAllFileFilterUsed(false);

答案 1 :(得分:0)

我做到了这一点并且有效。

if(!file1.exists())
{
           FileOutputStream fileOut =  new   FileOutputStream(file1+".xls");
           hwb.write(fileOut);
           fileOut.close();
           System.out.println("\n Your Excel file has been generated!");
           JOptionPane.showMessageDialog(this,"File Created.");
}