Java如何使用JFileChooser保存由ApachePOI创建的excel文件
我想将电子表格文件保存到用户自定义文件夹,人们建议使用Java如何使用JFileChooser保存由ApachePOI创建的excel文件,java,apache-poi,jfilechooser,Java,Apache Poi,Jfilechooser,我想将电子表格文件保存到用户自定义文件夹,人们建议使用JFileChooser,但我实际上不知道如何实现。我的当前示例代码如下: 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.
JFileChooser
,但我实际上不知道如何实现。我的当前示例代码如下:
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
似乎是个不错的选择,但有人能告诉我如何在我的案例中使用它吗
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();
}
当我们想用它来保存文件时,我们需要:
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();
}
以下是一个例子:
JFileChooser jfc = new JFileChooser();
int res = jfc.showSaveDialog(this);
if (res != JFileChooser.APPROVE_OPTION) {
return;
}
File file = jfc.getSelectedFile();
以下是一个例子:
JFileChooser jfc = new JFileChooser();
int res = jfc.showSaveDialog(this);
if (res != JFileChooser.APPROVE_OPTION) {
return;
}
File file = jfc.getSelectedFile();
public void importExcelToJtableJava(){
}public void importExcelToJtableJava(){
}打开用户自定义文件的非常好的示例。但是,我要求将我创建的文件保存到自定义文件位置。我假设我需要使用showSaveDialog而不是showOpenDialog。你能告诉我怎么做这个部分吗?是的,你说得对。其他参数与此相同。祝你编程愉快!现在可以了。非常感谢。但是,过滤器部件不能正常工作。它只包含“文件”,所以我必须用扩展名“.xlsx”来命名我的文件,否则它只会创建一个不可读的文件。打开用户自定义文件的非常好的示例。但是,我要求将我创建的文件保存到自定义文件位置。我假设我需要使用showSaveDialog而不是showOpenDialog。你能告诉我怎么做这个部分吗?是的,你说得对。其他参数与此相同。祝你编程愉快!现在可以了。非常感谢。但是,过滤器部件不能正常工作。它只包含“文件”,所以我必须用扩展名“.xlsx”命名我的文件,否则它只会创建一个不可读的文件。