0

I have a small problem. I want to display an excel file in the tableview. I would like to select the file with the filechooser and display it in the tableview.

I tried it with the teaxtarea as an example and it worked wonderfully. I need it in the tableview. I would be grateful if you could help me.

I program it with JavaFX

I'll show you how I did it with the textarea.

I have already created the tableview separately. I just need an example of how I get it to insert in the tableview.

I open the excel file using apache poi.

public class ReadExcel extends Application {

Pane pane = new Pane();
Button button = new Button("Hier");
Desktop desktop = Desktop.getDesktop();
TextArea textarea = new TextArea();

public void start(Stage primaryStage) throws IOException {

    Scene scene = new Scene(pane);

    scene.getStylesheets().add("style.css");
    primaryStage.setTitle("GBO-Tool");
    primaryStage.setScene(scene);
    primaryStage.show();
    textarea.setLayoutX(100);
    textarea.setLayoutY(100);

    pane.getChildren().addAll(button, textarea);

    FileChooser filechooser = new FileChooser();
    button.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent e) {
            File file = filechooser.showOpenDialog(primaryStage);
            if (file != null) {
                try {
                    readExcel();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
    });
}

public void readExcel() throws IOException {
    String excelfile = "Gesamtpunktzahl.xlsx";
    FileInputStream is = new FileInputStream(newFile(excelfile));
    Workbook wb = new XSSFWorkbook(is);
    Sheet sheet = wb.getSheetAt(0);
    Iterator<Row> iterator = sheet.iterator();
    while (iterator.hasNext()) {
        Row nextRow = iterator.next();
        Iterator<Cell> cellIterator = nextRow.cellIterator();

        while (cellIterator.hasNext()) {
            Cell cell = cellIterator.next();

            switch (cell.getCellType()) {
            case STRING:
                textarea.appendText(cell.getStringCellValue() + "\n");
                break;
            case BOOLEAN:
                textarea.appendText(cell.getBooleanCellValue() + "\n");
                break;
            case NUMERIC:
                textarea.appendText(cell.getNumericCellValue() + "\n");
                break;
            }
            textarea.appendText("\n");
        }
    }
    wb.close();
    is.close();
}

private static String newFile(String excelfile) {
    return excelfile;
    // TODO Auto-generated method stub

}

public static void main(String[] args0) {
    launch();

}

}

dornigB
  • 27
  • 6
  • Create an Object to represent the data. Then do the normal TableView stuff. – SedJ601 Feb 22 '19 at 15:53
  • represents the object the whole data of the tableview or is this a seperate data? – dornigB Feb 22 '19 at 16:17
  • 1
    In normal usage a `TableView` works by having a model class that exposes properties, ideally as `ObservableValue`s (e.g. `StringProperty`), and then each instance of that class is a row while (in basic cases) each property is a column. Also, take a look at the [spreadsheet control](https://controlsfx.bitbucket.io/org/controlsfx/control/spreadsheet/package-summary.html) from [ControlsFX](https://github.com/controlsfx/controlsfx). – Slaw Feb 22 '19 at 17:13
  • Look at this I created and write up your own answer. https://gist.github.com/sedj601/da2717d4d075b4f85f3c8322df6659b8 – SedJ601 Feb 22 '19 at 17:31
  • Thanks for helping me guys @Sedrick I have pasted your example in my code and i have a mistake somewhere. thats my code https://gist.github.com/busgi/9f3b856e0b3911a0a0b44916d1131439 – dornigB Feb 22 '19 at 19:52
  • this is my exception: Exception in thread "JavaFX Application Thread" java.lang.NoClassDefFoundError: org/apache/xmlbeans/XmlObject at MyProject.View.extractDataFromFile(View.java:220) at MyProject.View.lambda$0(View.java:208) at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86) at i have apache poi. why i have to use XMLBeans?? – dornigB Feb 22 '19 at 19:54
  • You need to make sure you add the Apache POI jars and all of its dependencies. – SedJ601 Feb 22 '19 at 19:56

0 Answers0