Java 如何使用 JFileChooser 保存由 Apache POI 创建的 excel 文件 [英] Java how to user JFileChooser to save a excel file created by Apache POI

查看:31
本文介绍了Java 如何使用 JFileChooser 保存由 Apache POI 创建的 excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将电子表格文件保存到用户自定义文件夹中,人们建议使用JFileChooser,但我实际上不知道如何实现.我在这里有我当前的示例代码:

I want to save a spreadsheet file to a user custom file folder, people suggest to use JFileChooser, but I actually don't know how to achieve it. I have my current example code here:

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 似乎是一个不错的选择,但有人可以告诉我如何在我的情况下使用它吗?

Currently it only save the file to the default project directory. But I want it to be saved to a user selected path with a user customized filename. JFileChooser seems like a good choice, but can someone show me how do I use it in my case?

推荐答案

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");
        }
    }

}

当我们想用它来保存文件时,我们需要:

When we want to use it to save a file, we need:

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();
}

这篇关于Java 如何使用 JFileChooser 保存由 Apache POI 创建的 excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆