-1

i am trying to save an XSSF workbook that i created. With

    FileOutputStream out = new FileOutputStream(new File("example.xlsx"));
    wb.write(out);
    out.close();

i able to save it the workspace. wb is the Workbook.

Now i want to save it using filechooser. I want the User to select the filepath trying this:

     FileChooser fileChooser = new FileChooser();
     Stage stage = new Stage();
     fileChooser.setTitle("Save Image");

     FileChooser.ExtensionFilter xlsxFilter = new 
     FileChooser.ExtensionFilter("XLSX files (*.xlsx)", "*.xlsx");
     FileChooser.ExtensionFilter xlsFilter = new 
     FileChooser.ExtensionFilter("XLS files (*.xls)", "*.xls");

     fileChooser.getExtensionFilters().add(xlsFilter);
     fileChooser.getExtensionFilters().add(xlsxFilter);

     System.out.println("save");
     File file = fileChooser.showSaveDialog(stage);

This chooses a saving path, but i dont know how to connect it with the workbook so that it can be saved. Any Ideas?

Thanks

DunKing
  • 1
  • 3
  • 1
    What's wrong with doing the same thing you did in your first code block? Create a `FileOutputStream` from the chosen file and pass it to `wb.write(...)`. – James_D Jul 10 '17 at 23:33

1 Answers1

1

This app creates an Excel workbook and writes to it. It then saves the workbook using FileChooser

import java.io.*;
import java.util.logging.*;
import javafx.application.*;
import javafx.event.*;
import javafx.scene.*;
import javafx.scene.control.*;
import javafx.scene.layout.*;
import javafx.stage.*;
import org.apache.poi.xssf.usermodel.*;

/**
 *
 * @author Sedrick
 */
public class JavaFXApplication57 extends Application {

    @Override
    public void start(Stage primaryStage)
    {
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("Java Books");

        //Data to write to Excel file.
        Object[][] bookData = {
            {"Head First Java", "Kathy Serria", 79},
            {"Effective Java", "Joshua Bloch", 36},
            {"Clean Code", "Robert martin", 42},
            {"Thinking in Java", "Bruce Eckel", 35},};

        int rowCount = 0;

        for (Object[] aBook : bookData) {
            XSSFRow row = sheet.createRow(++rowCount);

            int columnCount = 0;

            for (Object field : aBook) {
                XSSFCell cell = row.createCell(++columnCount);
                if (field instanceof String) {
                    cell.setCellValue((String) field);
                }
                else if (field instanceof Integer) {
                    cell.setCellValue((Integer) field);
                }
            }

        }

        Button btn = new Button();
        btn.setText("Save File");
        btn.setOnAction(new EventHandler<ActionEvent>() {

            @Override
            public void handle(ActionEvent event)
            {
                FileChooser fileChooser = new FileChooser();

                //Set extension filter to .xlsx files
                FileChooser.ExtensionFilter extFilter = new FileChooser.ExtensionFilter("Excel files (*.xlsx)", "*.xlsx");
                fileChooser.getExtensionFilters().add(extFilter);

                //Show save file dialog
                File file = fileChooser.showSaveDialog(primaryStage);

                //If file is not null, write to file using output stream.
                if (file != null) {
                    try (FileOutputStream outputStream = new FileOutputStream(file.getAbsolutePath())) {
                        workbook.write(outputStream);
                    }
                    catch (IOException ex) {
                        Logger.getLogger(JavaFXApplication57.class.getName()).log(Level.SEVERE, null, ex);
                    }
                }
            }
        });

        StackPane root = new StackPane();
        root.getChildren().add(btn);

        Scene scene = new Scene(root, 300, 250);

        primaryStage.setTitle("Hello World!");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args)
    {
        launch(args);
    }

}
SedJ601
  • 12,173
  • 3
  • 41
  • 59