Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Open Excel File with JavaFX

Tags:

java

excel

javafx

I'm making a project in which the user has to give in a document with data. The program reads the data and makes some diagrams. It al works perfectly but i want to open the ExcelFile when the user has saved it...So I know the directory of the Excelfile but i don't know how to open an excelfile in javaFX, can someone help me?

kind regards

like image 515
MiLo Avatar asked Sep 10 '25 10:09

MiLo


1 Answers

The JavaFX way to do this is

File excelFile = new File("/path/to/excel/file");
getHostServices().showDocument(excelFile.toURI().toURL().toExternalForm());

getHostServices() is defined in Application, so if you want to do this in another class (a controller, for example), you will have to arrange for the other class to be able to access the host services.

E.g.

public class MyApp extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader(...);
        Parent root = loader.load();
        MyController controller = loader.getController();
        controller.setHostServices(getHostServices());
        //... setup and show scene and stage...
    }
}

With the obvious method in the controller and the code above suitably modified.

like image 85
James_D Avatar answered Sep 13 '25 01:09

James_D