我正在制作一个项目,用户需要提供一份包含数据的文档。程序会读取数据并生成一些图表。一切都运行得很完美,但是我想在用户保存Excel文件后打开它...所以我知道Excel文件的目录,但我不知道如何在JavaFX中打开Excel文件,有人可以帮帮我吗?
此致敬礼
此致敬礼
File excelFile = new File("/path/to/excel/file");
getHostServices().showDocument(excelFile.toURI().toURL().toExternalForm());
getHostServices()
方法在Application
中定义,因此如果您想在另一个类中(例如控制器)执行此操作,您需要为其他类安排访问主机服务的权限。
例如:
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...
}
}
通过在控制器中使用明显的方法并适当修改上述代码。