Java如何使用JFileChooser来保存Apache POI创建的excel文件
我想将电子表格文件保存到用户自定义文件夹,人们建议使用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 jfc = new JFileChooser(); int res = jfc.showSaveDialog(this); if (res != JFileChooser.APPROVE_OPTION) { return; } File file = jfc.getSelectedFile();
- 在使用JPA持久化时是否需要设置实体类的所有字段?
- HttpURLConnection即使在setDoOutput(true)之后执行get请求,setRequestMethod(“POST”)setRequestProperty(“Content”
- 更新Apache POI 4.0后 – 无法识别属性“http://www.oracle.com/xml/jaxp/properties/entityExpansionLimit”
- 通过Apache POI读取大型Excel文件(xlsx)时出错
- 如何将excel中的值存储到java中的某些集合
- Apache Poi:获取DOC文档中的页数
- java POI XSSF FormulaEvaluator
- 如何使用合并单元格值大于单元格宽度的Apache-POI来增加excel行的高度?
- 如何在Java中为word文档(.doc或.docx)设置背景颜色(页面颜色)?
- Apache POI与Android – 如何创建,读取,写入,删除PowerPoint演示文稿?
- 如何使用apache poi删除一行