2017-05-19 85 views
1

我想將電子表格文件保存到用戶自定義文件夾中,人們建議使用JFileChooser,但我其實不知道如何實現它。我在這裏有我目前的示例代碼:Java如何使用JFileChooser保存由Apache POI創建的excel文件

import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import org.apache.poi.xssf.usermodel.XSSFCell; 
import org.apache.poi.xssf.usermodel.XSSFRow; 
import org.apache.poi.xssf.usermodel.XSSFSheet; 
import org.apache.poi.xssf.usermodel.XSSFWorkbook; 

public class Example { 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
     XSSFWorkbook workbook; 
     File file = new File("example.xlsx"); 
     if (file.exists() == false) { 
      workbook = new XSSFWorkbook(); 
      XSSFSheet exampleSheet = workbook.createSheet("1"); 
      XSSFRow firstRow = exampleSheet.createRow(1); 
      XSSFCell cell = firstRow.createCell(0); 
      cell.setCellValue("value"); 

      try ( 
       //Write the workbook in file system 
       FileOutputStream out = new FileOutputStream(file)) {    
        workbook.write(out); 
       } 
     } else { 
      // Sheet already exists 
      System.out.println("File already exist"); 
     } 
    } 

} 

目前它只保存文件到默認項目目錄。但我希望它用用戶自定義的文件名保存到用戶選擇的路徑。 JFileChooser似乎是一個不錯的選擇,但有人可以告訴我如何在我的情況下使用它?

回答

2
public class Example { 
    static String fileDictName = ""; 

    public static void main(String[] args) throws FileNotFoundException, IOException { 
     XSSFWorkbook workbook; 

     JFileChooser fileChooser = new JFileChooser(); 
     fileChooser.setDialogTitle("Open the file"); //name for chooser 
     FileFilter filter = new FileNameExtensionFilter("Files", ".xlsx"); //filter to show only that 
     fileChooser.setAcceptAllFileFilterUsed(false); //to show or not all other files 
     fileChooser.addChoosableFileFilter(filter); 
     fileChooser.setSelectedFile(new File(fileDictName)); //when you want to show the name of file into the chooser 
     fileChooser.setVisible(true); 
     int result = fileChooser.showOpenDialog(fileChooser); 
     if (result == JFileChooser.APPROVE_OPTION) { 
      fileDictName = fileChooser.getSelectedFile().getAbsolutePath(); 
     } else { 
      return; 
     } 

     File file = new File(fileDictName); 
     if (file.exists() == false) { 
      workbook = new XSSFWorkbook(); 
      XSSFSheet exampleSheet = workbook.createSheet("1"); 
      XSSFRow firstRow = exampleSheet.createRow(1); 
      XSSFCell cell = firstRow.createCell(0); 
      cell.setCellValue("value"); 

      try (
        //Write the workbook in file system 
        FileOutputStream out = new FileOutputStream(file)) { 
       workbook.write(out); 
      } 
     } else { 
      // Sheet already exists 
      System.out.println("File already exist"); 
     } 
    } 

} 

當我們想用它來保存文件,我們需要:

JFileChooser fileChooser = new JFileChooser(); 
FileFilter filter = new FileNameExtensionFilter("Files", ".xlsx"); 
fileChooser.addChoosableFileFilter(filter); 
fileChooser.setAcceptAllFileFilterUsed(false); 
fileChooser.setDialogTitle("Save the dictionary file"); 
fileChooser.setSelectedFile(new File(fileDictName)); 
int userSelection = fileChooser.showSaveDialog(fileChooser); 
if (userSelection == JFileChooser.APPROVE_OPTION) { 
    fileDictName = fileChooser.getSelectedFile().getAbsolutePath(); 
} 
+0

真的好樣打開一個用戶自定義文件。但是,我要求將我創建的文件保存到自定義文件位置。我假設我需要使用showSaveDialog而不是showOpenDialog。你能告訴我怎麼做這部分? –

+0

是的,你是對的。其他參數相同。祝您節目愉快! –

+0

它現在有效。謝謝。但是,過濾器部件無法正常工作。它只包含「文件」,所以我必須用「.xlsx」擴展名命名我的文件,否則它只會創建一個不可讀的文件。 –

1

下面是一個例子:

JFileChooser jfc = new JFileChooser(); 
int res = jfc.showSaveDialog(this); 
if (res != JFileChooser.APPROVE_OPTION) { 
      return; 
} 
File file = jfc.getSelectedFile(); 
相關問題